Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unity SetActive(true) not working after setting it to false at first?

Tags:

c#

unity3d

Heirarchy structure

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);

        }
    }

As you can see above I have disable "Touch Panel" at start function. But on DropBall function ( remember it is attached to a button), I have set it to active but it is not working.Can anyone help me with this.Thanks.

EDIT:- This script is attached on a "Ball". and ball is attached on a Button. "Touch Panel" is child of Canvas.

like image 828
Samrat Luitel Avatar asked Mar 10 '23 10:03

Samrat Luitel


1 Answers

Your problem is that when you disable your Touch Panel , GameObject.Find will not find it so you won't be able to enable it again.

From the documentation:

This function only returns active GameObjects.

You need to setup your Touch Panel as a public GameObject myPanel and assign it in the inspector then you can enable and disable it.

myPanel.SetActive(false);
myPanel.SetActive(true);
like image 115
I.B Avatar answered Apr 08 '23 23:04

I.B