- Home /
SetActive and prefabs not working
I have a scirpt where, on the end of each of my rounds, a Gameobject spawns and deletes within 5 seconds. this gameobject has a script to set the gui image for "round 1" inactive, and the gui image for "round 2" to active. When I press play, it doesn't seem to work. However if I (with the game still running) go into the scene view and look, it's all going as planned. round 2 instead of round 1, the gameobject despawned etc. What I can't figure out is why the changes to my gui didn't show up in my game view.
the only problem I could think of would be if there's a problem with setting the prefab true, and if that doesn't change in the scene view. I don't know, some input would be very helpful at this point.
p.s the script is just a basic set one (public) gameobject to false and invoke to set the other active after a second.
If I understood well you see changes in scene view but not in game view? Changes in the UI or changes in the GameObject? Can you give some source code to evaluate?
I do see it in the scene view, not the game view. and yes it's a UI object. (raw image)
heres the code I use to set the gameobject to active
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class Roundx3 : $$anonymous$$onoBehaviour {
public static GameObject RoundLast;
public static GameObject RoundNext;
// Use this for initialization
void Start()
{
}
// Update is called once per frame
void Update()
{
RoundLast.SetActive(false);
Invoke("Round_2", 1f);
}
public void Round_2()
{
RoundNext.SetActive(true);
}
}
so when the gameobject spawns with this code attached to it, the "last" round goes away and the next one appears. or atleast it should
Answer by beppim · Nov 06, 2015 at 04:31 PM
Uhm. The first thing that I note is that you set the two objects as static. How do you initialize them? Why don't you set as non static and initialize them right from the Inspector? Anyway, supposing that they have been initialized elsewhere, and the problem is not there, I tried your code and, providing the fact that the two objects are set inactive before running the code, the thing works. Maybe one other problem is that you put the code inside the Update() that runs at every frame: how many times will that code be called? Put it inside the Start() or in the Awake(). This code works to me:
using UnityEngine;
using System.Collections;
public class NewBehaviourScript : MonoBehaviour {
public GameObject RoundLast;
public GameObject RoundNext;
void Start()
{
RoundLast.SetActive(false);
Invoke("Round_2", 3f);
}
public void Round_2()
{
RoundNext.SetActive(true);
}
}
with a Canvas with two UI objects as children.
But why instantiate an object to run a code? Wouldn't it be better to create a code and call it via a public method?
This should work anyway, creates a public method callable by outside code and uses the coroutines to wait.
public class NewBehaviourScript : MonoBehaviour {
public void SwitchScene(GameObject round1, GameObject round2)
{
StartCoroutine(ChangeScene(round1, round2));
}
IEnumerator ChangeScene(GameObject round1, GameObject round2)
{
round1.SetActive(false);
yield return new WaitForSeconds(3f);
round2.SetActive(true);
}
}
The problem with this is that I'm using something called Core Gamekit, and it lets you spawn prefabs during each round. So when the new round starts, I would just spawn the prefab that changes the round image.
For some reason, it still doesnt work though.
$$anonymous$$aybe the problem is that Core Gamekit loads everything into a pool on the first frames, and then just sets them to active later. maybe that makes an error with the script somehow.
I don't know, I have a few ideas where to go from here so thanks for your time and help.
With some Debug.Log() you can detect if the Start() and Awake() are not triggered when you expect, and use other functions like OnRenderObject() or OnEnable()
Your answer
![](https://koobas.hobune.stream/wayback/20220612044128im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Problems with save 1 Answer
Activating a prefab in the scene by find it over a tag? 1 Answer
Instantiate a GameObject from a Prefab without using an original to clone 1 Answer
Is it possible to edit the sub-sub-GameObjects of a prefab in the assets window? 1 Answer
Prefab not loaded in webplayer!? 2 Answers