Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by TimeCastle · Nov 06, 2015 at 02:36 PM · guigameobjectprefabsetactivetrue

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.

Comment
Add comment · Show 2
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image beppim · Nov 06, 2015 at 03:45 PM 0
Share

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?

avatar image TimeCastle beppim · Nov 06, 2015 at 04:08 PM 0
Share

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

1 Reply

· Add your reply
  • Sort: 
avatar image
2
Best Answer

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);
     }
 
 }
 
Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image TimeCastle · Nov 09, 2015 at 04:26 PM 0
Share

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.

avatar image beppim TimeCastle · Nov 12, 2015 at 05:01 PM 0
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

47 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges