简体   繁体   English

来自Unity3D中另一个脚本的布尔值

[英]boolean from another script in Unity3D

So I've got two scripts, one is called EndCollider.cs it's somewhere on the map, It have one OnTriggerEnter function which set a boolean to true. 所以我有两个脚本,一个叫做EndCollider.cs,它在地图上,它有一个OnTriggerEnter函数,它将布尔值设置为true。

using UnityEngine;
using System.Collections;

public class EndCollider : MonoBehaviour {

public bool isShow = false;

void OnTriggerEnter ()
{
    isShow = true;
}

}

the other script is SlowDownRun.cs, it's on a monster object, in this script I'm trying to detect if the boolean from another script is true or not, if it is then move the monster. 另一个脚本是SlowDownRun.cs,它在一个怪物对象上,在这个脚本中,我试图检测另一个脚本中的布尔值是否正确,然后再移动怪物。

using UnityEngine;
using System.Collections;

public class SlowDownRun : MonoBehaviour {

GameObject TrollScript;
EndCollider MonsterShow;

// Use this for initialization
void Start ()
{
    TrollScript = GameObject.Find("Troll");
    MonsterShow = TrollScript.GetComponent<EndCollider>();
}

void Update()
{
    if (MonsterShow.isShow == true)
    {
        float movementSpeed = 10f;
        transform.position += transform.forward * Time.deltaTime * movementSpeed;
    }
}

}

Now the code doesn't work, the code where move the monster works if I take them out the IF statement. 现在该代码不起作用,如果我从IF语句中将它们移走,则该代码可以移动怪物。 I also keep getting this error NullReferenceException: Object reference not set to an instance of an object on the line if (MonsterShow.isShow == true) 我还不断收到此错误NullReferenceException:如果(MonsterShow.isShow == true),则对象引用未设置为对象的实例

Please help I'm a beginner to Unity and this problem stuck on me for so many hours now, I've done a lot Google searches and modified my code again and again but just can't solve this problem, I feel anxious and just don't know what to do. 请帮助我是Unity的初学者,这个问题困扰了我好几个小时,我做了很多Google搜索并一次又一次地修改了我的代码,但只是无法解决此问题,我感到焦虑而已不知道该怎么办。

Your find troll call in the Start() method is not finding the troll or not finding the EndCollider component on it, resulting in it being null. 您在Start()方法中的find troll调用未找到该troll或未在其上找到EndCollider组件,从而导致该结果为null。 And since you only search once at startup it will always be null. 而且由于您在启动时仅搜索一次,因此它将始终为null。

This is probably because your troll is not yet created at the start of the scene. 这可能是因为尚未在场景开始时创建您的巨魔。

If your intent is actually to make the monster appear when the player hit the collider, you should look at Prefab Instantiation. 如果实际上是要让玩家在撞到对撞机时让怪物出现,则应该查看“预制实例化”。

http://docs.unity3d.com/Manual/InstantiatingPrefabs.html http://docs.unity3d.com/Manual/InstantiatingPrefabs.html

So in your OnTriggerEnter, you can simply instantiate a brand new monster: 因此,您可以在OnTriggerEnter中实例化一个全新的怪物:

public class EndCollider : MonoBehaviour {

public GameObject MonsterPrefab;

void OnTriggerEnter ()
{
    Instantiate(MonsterPrefab);
}

}
  • Make masterShow endcollider public 将masterShow endcollider公开
  • clean your start method. 清理您的启动方法。
  • Just drag and drop the troll object from hierarchy to your script. 只需将巨魔对象从层次结构拖放到脚本中即可。

    public class SlowDownRun : MonoBehaviour { 公共类SlowDownRun:MonoBehaviour {

    public EndCollider MonsterShow; 公共EndCollider MonsterShow;

    // Use this for initialization void Start () { //将其用于初始化void Start(){

    } }

    void Update() { if (MonsterShow.isShow == true) { float movementSpeed = 10f; void Update(){if((MonsterShow.isShow == true){浮动运动速度= 10f; transform.position += transform.forward * Time.deltaTime * movementSpeed; transform.position + = transform.forward * Time.deltaTime * MovementSpeed; } } }}

    } }

One better way of communicating data between two scenes might be using PlayerPrefs: 在两个场景之间传递数据的一种更好的方法可能是使用PlayerPrefs:

//EndCollider.cs
public bool isShown
{
    get { return PlayerPrefs.GetInt("IS_SHOWN")==1? true:false; }
    set { PlayerPrefs.SetInt("IS_SHOWN", Convert.ToInt32(value));} 
}

//SlowDownRun.cs
if(PlayerPrefs.GetInt("IS_SHOWN")==1)  // isShown = true
{
    ...
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM