I have a question, how can i activate a gameobject ? I can easly deactivate it with this code:
void Start ()
{
gameObject.SetActive(false);
}
But i can’t activate an object if it is deactivated because the script then won’t work so i can’t just write: gameObject.SetActive(true);
Can someone please help me ?
Well, If you have this script on the GameObject that you want to activate/deactivate, this won`t be possible.
I suggest you to put this script in another game object and declare it with tag or public gameobject.
using UnityEngine;
using System.Collections;
public class ActivateObject: MonoBehaviour {
public GameObject NameOfGameObjectThatYouWantToActivate;
void Start() {
NameOfGameObjectThatYouWantToActivate.SetActive(true);
NameOfGameObjectThatYouWantToActivate.SetActive(false);
}
}
Or with tag:
using UnityEngine;
using System.Collections;
public class ActivateObject: MonoBehaviour {
private GameObject NameOfGameObjectThatYouWantToActivate;
void Start() {
NameOfGameObjectThatYouWantToActivate = GameObject.FindGameObjectsWithTag("TagYouPutInYourGameObject");
NameOfGameObjectThatYouWantToActivate.SetActive(true);
NameOfGameObjectThatYouWantToActivate.SetActive(false);
}
}
I hope helped you!