触发对象时是否显示文字消息?

问题描述

我正在制作一个我想要的图书馆互动地图,以便当玩家接触并触发围绕地图的不可见对象时,显示消息的文本将落到他的视图中,然后他可以按Enter键当我尝试运行代码时,控制台将返回“我无法修改'Transform.position'的返回值,因为它不是变量”,因此它将备份。也许这与文本是“ RectTranform”而不是“ Transform”有关,我不知道

以下是触发代码:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
//I misspelled collision but that doesn't matter
public class collsiion : MonoBehaviour
{
    void OnTriggerEnter(){
         FindObjectOfType<messageToggle>().goingdown();
    }
     
}

这是消息移动代码:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class messageToggle : MonoBehaviour
{
   public RectTransform _message;

   public bool gru = false;

   public float brumium = 50f;
   public void goingdown(){
        _message.posY =- brumium;
        gru = true;
   }
   void Update()
    {
        if(gru == true){
            if(Input.GetKey("f")){
                //by the way does anyone know how to put Enter key as an input
                _message.position.y =+ brumium;
            }     
        }
    }
}    

解决方法

由于Transform.position是属性,并且在访问时,将其作为结构体返回一个副本。为避免引发错误,您需要重新分配位置向量:

_message.position = new Vector3(_message.position.x,_message.position.y + brumium,_message.position.z);

或者,您可以翻译:(https://docs.unity3d.com/ScriptReference/Transform.Translate.html

相关问答

依赖报错 idea导入项目后依赖报错,解决方案:https://blog....
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下...
错误1:gradle项目控制台输出为乱码 # 解决方案:https://bl...
错误还原:在查询的过程中,传入的workType为0时,该条件不起...
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct...