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.
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);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With