Unity SetActive(true)在首先将其设置为false后无法正常工作?

Sam*_*tel 4 c# unity-game-engine

Heirarchy结构

using UnityEngine;
    using System.Collections;

    public class Ball : MonoBehaviour {
        private Rigidbody rigidbody;
        public Vector3 LaunchVelocity;
        private AudioSource audiosource;

        // Use this for initialization
        void Start ()
        {
            GameObject.Find("Touch Panel").SetActive(false);
            rigidbody = GetComponent<Rigidbody>();
            rigidbody.isKinematic = true;
            // disable touch control
            audiosource = GetComponent<AudioSource>();
        }



        // Update is called once per frame
        void Update()
        {
        }

        public void LaunchBall(Vector3 passedvelocity)
        {
            rigidbody.velocity = passedvelocity;
        }

        public void DropBall()  // This is attached to a button
        {
            rigidbody.isKinematic = false;
            GameObject.Find("Touch Panel").SetActive(true);

        }
    }
Run Code Online (Sandbox Code Playgroud)

如您所见,我在启动功能时禁用了"触摸屏".但是在DropBall功能上(请记住它附加到一个按钮),我已将其设置为活动状态,但它无法正常工作.任何人都可以帮助我.谢谢.

编辑: - 此脚本附加在"球"上.并且球附在按钮上."触摸屏"是画布的孩子.

I.B*_*I.B 6

您的问题是,当您禁用时Touch Panel,GameObject.Find将无法找到它,因此您将无法再次启用它.

从文档:

此函数仅返回活动的GameObjects.

您需要将您设置Touch Panel为a public GameObject myPanel并在检查器中分配它,然后您可以启用和禁用它.

myPanel.SetActive(false);
myPanel.SetActive(true);
Run Code Online (Sandbox Code Playgroud)