【问题标题】:Can't access variable from another script无法从另一个脚本访问变量
【发布时间】:2022-07-16 00:40:39
【问题描述】:

Assets\Scripts\Wood.cs(32,9): error CS0201: 只有赋值、调用、递增、递减、等待和新对象表达式可以用作语句

我正在尝试将布尔值 hasTorch 放入脚本 Wood.cs 以了解玩家是否有手电筒。

(我是新手,所以可能很容易修复,我只是不知道:c)

脚本 1 (Chest.cs):

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

public class Chest : MonoBehaviour
{
    public Sprite openChest;

    public GameObject chest1;
    public GameObject chestBox;
    public GameObject torch;

    public bool hasTorch = false;

    public GameObject darkness;
    public GameObject chatBox;

    private void OnTriggerEnter2D(Collider2D other)
    {
        chest1.GetComponent<SpriteRenderer>().sprite = openChest;
        torch.SetActive(true);
        chatBox.SetActive(true);
        darkness.SetActive(false);
        chestBox.SetActive(false);

        hasTorch = true;
    }

    public void Close()
    {
        chatBox.SetActive(false);
    }

}

脚本 1 (Wood.cs):

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

public class Wood : MonoBehaviour
{

    public GameObject chestScript;
    public Chest script;

    public GameObject chatBox;

    private void OnTriggerEnter2D(Collider2D other)
    {
        if (script.hasTorch == true)
        {
            chatBox.SetActive(true);
        }

        if (script.hasTorch == true)
        {
            chatBox.SetActive(true);
        }
    }

    public void Close()
    {
        chatBox.SetActive(false);
    }

    void Start(){
        chestScript.GetComponentInChildren<Chest>().hasTorch;
    }

}

【问题讨论】:

    标签: c# unity3d


    【解决方案1】:

    这一行没有做任何事情:

    chestScript.GetComponentInChildren<Chest>().hasTorch;
    

    您可以像这样记录它或将其设置为真/假:

    chestScript.GetComponentInChildren<Chest>().hasTorch = false;
    

    【讨论】:

      猜你喜欢
      • 2021-10-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-11-22
      • 1970-01-01
      • 2021-05-20
      相关资源
      最近更新 更多