- Home /
Unable to reactive the game object
Hi, I'm new in Unity, I'm create a plant growing simulation game on android platform. I'm using texture button as my button to trigger a game objects. I was able to set it into inactive state, but unable to reactive it... The following is part of my code in GUI Texture.
public class FertilizerBTN : MonoBehaviour {
public static int currTouch = 0; //so other scripts can know what touch is currently on screen
// Use this for initialization
public GameObject fer;
void Start () {
fer = GameObject.Find("Fertilizers");
fer.SetActive(false);
}
// Update is called once per frame
void Update () {
if(Input.touches.Length <= 0){
//if no touches then execute this code
}
else //if there is a touch
{
//loop through all the the touches on screen
for(int i = 0; i < Input.touchCount; i++)
{
currTouch = i;
//executes this code for current touch (i) on screen
if(this.guiTexture != null && (this.guiTexture.HitTest(Input.GetTouch(i).position)))
{
//if current touch hits our guitexture, run this code
if(Input.GetTouch(i).phase == TouchPhase.Began)
{
fer = GameObject.Find("Fertilizers");
fer.SetActive(true);
Debug.Log("OnTouchBegan");
}
if(Input.GetTouch(i).phase == TouchPhase.Ended)
{
this.SendMessage("OnTouchEnded");
}
if(Input.GetTouch(i).phase == TouchPhase.Moved)
{
this.SendMessage("OnTouchMoved");
}
}
}
}
}
}
I'm sorry if I'm done some newbie mistakes... Hope to get help soon, please and thank you.
erm, no, it's only a game object name... It's a must to have a script on the game objects?
Answer by Joyrider · Aug 19, 2013 at 03:17 PM
You cannot get an inactive gameObject with the GameObject.Find function. (it is explicitly mentionned in the scripting reference)
Before disabling thoses objects, you should store them in a variable/array, or know of a way to access them through the hierarchy.
Right. Just don't do the Find each time. Do it in Start (setting 'fer') then you should be able to de/activate it.
Thanks for the helps, It works ! I cant believe this is the mistake I made...Anyway, much appreciate. =)