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
0
Question by ethranes · Aug 17, 2016 at 03:23 PM · c#tagscene-switchingsetactivefindgameobjectswithtag

setActive on object in separate scene

Hi,

I have 2 scenes a game scene and a menu screen, I have an object visible on the menu which I want to disable when the player loads from level 1 back into the menu. This is the code that I'm using which is attached to the level scene.

 SceneManager.LoadScene("sceneSelectBeta");
             GameObject[] gameObjectArray = GameObject.FindGameObjectsWithTag ("Level2Block");
 
             foreach(GameObject go in gameObjectArray)
             {
                 go.SetActive (false);
             }

So the scene should switch back to sceneselectBeta and then disable the object with the tag. I suppose the reason at the moment is because my code isn't influencing that tag when it's not present in the current scene. Alternatively is it possible to run the code on the menu scene, specifying that if the previous scene was the level scene, to run the code?

Thanks for any help or advice you guys can give. Cheers.

Comment
Add comment
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

2 Replies

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

Answer by ScaniX · Aug 17, 2016 at 03:42 PM

Is the game object in the level scene marked as DontDestroyOnLoad()?

Otherwise your code will not be executed. You will then have to DestroyObject(gameObject) manually afterwards.

 DontDestroyOnLoad(gameObject);
 SceneManager.LoadScene("sceneSelectBeta");
 GameObject[] gameObjectArray = GameObject.FindGameObjectsWithTag ("Level2Block");
 foreach(GameObject go in gameObjectArray) {
     go.SetActive (false);
 }
 DestroyObject(gameObject);
Comment
Add comment · Show 4 · 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 ethranes · Aug 17, 2016 at 05:31 PM 0
Share

Thanks for the suggestion, I've tried implementing this, so in my game scene I have a game object with the script. And then in my menu scene I have DestroyObject(gameObject);

Which does destroy the object but it also makes it so that none of the UI can be interacted with anymore.

avatar image ScaniX ethranes · Aug 17, 2016 at 06:21 PM 0
Share

No, this whole script is supposed to be attached to the level game object where your script from the question was co$$anonymous$$g from. You really just had to add those two lines. :)

avatar image ethranes ScaniX · Aug 17, 2016 at 06:35 PM 0
Share

Ahh great, yeah it's working perfectly. do you by any chance know how I can save the setActive state using PlayerPrefs, at the moment the gameObject becomes active each time the game is reloaded, but it should stay hidden.

Show more comments
avatar image
1

Answer by EDevJogos · Aug 17, 2016 at 03:43 PM

You can create a static variable to check if the player came from the gameScene:

 public static bool fromGame = false;

Then somewhere in your menu in a Start method you set the active = to !fromGame, like.

 GameObject[] gameObjectArray = GameObject.FindGameObjectsWithTag ("Level2Block");
  
 foreach(GameObject go in gameObjectArray)
 {
        go.SetActive (!fromGame);
 }

And in some start of your gameScene you set fromGame to true.

 ScriptThatHoldsTheVariable.fromGame = true;


void Start() is always called when a scene loads so the gameObjects active state in your array will be seted according to the fromGame value, when you go back to the menuScene.

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 ethranes · Aug 17, 2016 at 05:56 PM 0
Share

Thanks, this seems like a good way to do it, I'm unsure how to implement it though,

public static bool fromGame = false; - I have this in my game scene

then in the start function on my menu script I have the script to find the tag and set active.

I'm unsure where ScriptThatHoldsTheVariable.fromGame = true; fits in though.

avatar image EDevJogos ethranes · Aug 17, 2016 at 06:07 PM 0
Share

to access a static variable from another script in another scene you have to first say which script it belongs too:

Like if you declared public static bool fromGame = false; inside $$anonymous$$enu.cs than to call it on Game.cs you have to type:

 $$anonymous$$enu.fromGame = true;

The ideia is that if you have started the gameScene you change this variable to true, so that when you go back to the menuScene your objects will be seted to false. since the setActive do the opposite of the fromGame value:

 go.SetActive (!fromGame);

@ethranes

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

209 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 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 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 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 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

strange behaviour when creating and deleting objects with tags 1 Answer

Count the contents of the list from FindGameObjectsWithTag 1 Answer

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

How do I get a U.I. image to remember if it is active or Not Active Across Scene Changes Using Arduino 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