- Home /
GameObject.setActive(bool) is not activating my gameObject
Hi, I am running in to a problem in unity ,when the player pressed this button, the gameObject "thing", (already dragged in to the prefab), would show up, and covers the whole screen to "punish" this player for pressing this button.
thing is just a sprite sheet. The point is so that the player cannot play the game temporarily.
Two Problems occurs,
one, eventhough the object .activeself returns true in the debug console, the sprite never showed up, and i debugged it by time.timescale=0 ( to pause the game), the object's box is unchecked in the inspector window.
two, I wrote the code in another version where the object was always non-active(unchecked from the start), and upon punish, set active, that worked, but I couldn't get it to go away ( thing.gameObject.setActive (false);) did not make it go away.
BUT ALSO, the buttons were still accessible, which is not what I want, since I want "thing" to cover the player's window to "punish" the player.
Can anyone help please? If there is another way to "punish" the player(disable all button inputs, and show a sprite), please let me know.
Part of the relevant codes:
public gameObject thing;
bool punish = false;
void Start(){
thing.gameObject.setActive(false);
}
private void OnGUI () {
if (GUI.Button (nextB, ""){
punish=true;
}
}
void Update(){
if (punish) {
float time = 0;
while (time < 1) {
red.gameObject.SetActive(true);
Debug.Log ("entered"); //Debug console shows enter
Debug.Log (red.gameObject.activeSelf); //Debug console show true
//Time.timeScale=0; to debug Object is unchecked at inspector window
time = time + Time.deltaTime;
}
punish = false;
red.gameObject.SetActive (false);
}
}
Answer by vinod.kapoor · Jun 04, 2014 at 05:34 AM
public GameObject thing;
bool punish = false;
private float time;
void Start(){
thing.gameObject.setActive(false);
time = 0;
}
private void OnGUI () {
if (GUI.Button (nextB, ""){
punish=true;
time = 0;
red.gameObject.SetActive(true);// i dont know wheather it is should be //red.gameObject.SetActive(true) or thing.gameObject.SetActive(true)
}
}
void Update(){
if (punish) {
if(time < 1) {
Debug.Log ("entered"); //Debug console shows enter
time = time + Time.deltaTime;
} else{
punish = false;
red.gameObject.SetActive (false);// i dont know wheather it is should be //red.gameObject.SetActive(true) or thing.gameObject.SetActive(true)
}
} }
hope this is the code u require ............ :)
This looks like the right answer, but doesn't explain the problem.
Your problem is the while loop. You will just add Time.deltaTime to time as many times as required in a single frame, then SetActive(false) straight after. This code correctly moves the time variable to the instance scope and updates in in Update.
If you want to use while, you will need to make a coroutine or make Update an IEnumerator and use yield null;
inside the loop. Note that would could get rid of the loop by using something like yield return new WaitForSeconds(1f);
Generally it is better to use coroutines than Update to implement this kind of logic, but Update is much easier for beginners to get their head around.
And it still contains some errors I pointed out in my answer. But I don't see any while in his code...?
Thanks so much, that indeed did the trick, but the button is still infront of the "red" sprite.
Sorry. I meant the while in the original code! I didn't study vinod's code but it looks like it solves the main problem with the OPs code, but didn't explain what the problem was or how it was solved, so I just wanted to add a little detail to his response ;)
I don't see your answer fafase :s
use if ((GUI.Button (nextB, "")&& punish){
ins$$anonymous$$d of if (GUI.Button (nextB, ""){
Your answer
Follow this Question
Related Questions
Calling setActive(false) on a disabled object? 3 Answers
How to active/deactive gameobject? 1 Answer
Activating GameObjects one by one 2 Answers
Alternatives to GameObject.SetActive(...) 2 Answers
Script only works onetime 0 Answers