How do you activate a game object ?
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 ?
Answer by Rivewexe · Oct 28, 2015 at 12:19 PM
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!
Thank you, but i have another problem i made this code work with void Update () { if (Input.Get$$anonymous$$ey ($$anonymous$$eyCode.Space)) { Cube.SetActive(true); } else { Cube.SetActive(false); } }
but not with
void Update () { if (Input.Get$$anonymous$$eyDown ($$anonymous$$eyCode.Space)) { Cube.SetActive(true); } else { Cube.SetActive(false); } }
Can you please help ?
What my code that doesnt work does is that when i press space my gamobject gets active. but gameobjects parent gets deactivetet
Your answer
Follow this Question
Related Questions
My game objects aren't working as I expected them to. 1 Answer
How to enable script on another gameobject in C#? 1 Answer
How do i convert game object counts, into int, and put into string? 0 Answers
Cascading dropdown menus 0 Answers
SetActive is just working if i press twice in the first time 2 Answers