GameObject setActive null错误

时间:2017-01-07 10:44:04

标签: c# unity3d unity5

我有一个名为snow的gameObject,它基本上是一个粒子系统,我想要做的是,当我的玩家通过积雪的墙壁时,它会开始禁用。

要做到这一点,我开始将游戏对象禁用,就像我说的那样,当球与墙壁相撞时,我想激活雪。 喜欢这样:

using UnityEngine;
using System.Collections;
using UnityEngine.UI;
using UnityEngine.SceneManagement;

public class cameraChange : MonoBehaviour {

    private Camera focusCamera;
    private Camera mainCamera;
    private MeshFilter WallTurn;
    public GameObject snow;
    // Use this for initialization
    void Start () {
        focusCamera = GameObject.Find("ModeCamera").GetComponent<Camera> ();
        mainCamera = GameObject.Find("Main Camera").GetComponent<Camera> ();
        WallTurn = GameObject.Find ("WallTurn").GetComponent<MeshFilter> ();
    }

    // Update is called once per frame
    void OnTriggerEnter (Collider other) {
        Debug.Log (snow);
        if (other.gameObject.tag == "Player") {
            focusCamera.enabled = false;
            mainCamera.enabled = true;
            WallTurn.transform.Rotate (0f, 0f, 180f);
            snow.SetActive (true);

    }

问题是我收到此错误对象引用没有设置为对象的实例,我在检查器上分配了雪对象,而在Debug.log中它检测到它是一个游戏对象,我做错了什么?

1 个答案:

答案 0 :(得分:0)

你的GameObject snow被宣布但从未实例化。

在使用SetActive之前使用https://docs.unity3d.com/ScriptReference/Object.Instantiate.html进行实例化。