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 ilmix123 · Mar 11, 2017 at 11:47 AM · scripting problemeditorpublic variable

Problem when using script in multiple scenes.

Not really a question, just something I noticed that might or might not be a bug.

I have two scenes "Level1" and "Tutorial". Both scenes have an object called "Loader" (not a prefab, just two different objects in two different scenes). Both loader objects share one script (Loader.cs). This script has public variables that are assigned in the editor.

For some reason both scenes use the values assigned in the scene "Level1", (so values assigned in the scene "Tutorial" are ignored).

Is this a bug or is it by design?

Edit: The code of the Loader.cs. If I set test to 5 in "Tutorial" scene and 10 in "Level1" both scenes print out Test in this scene is: 10

 using UnityEngine;
 using System.Collections;
 
 public class Loader : MonoBehaviour {
     public int test;
     public Player player;
     public Canvas hudCanvas;
     public GameObject connectingPanelPrefab;
     [HideInInspector]public static bool isEndOfTurn = false; //is set to true when player ends turn and actions are beeing processed/displayed
     public GameObject AIPrefab;
 
     //Maps
     public GameObject map1Prefab;
     public GameObject tutorialMapPrefab;
 
     void Awake () {
         Debug.Log ("Test in this scene is: " + test);
 
         //create the connecting panel
         GameObject connectingPanel = Instantiate (connectingPanelPrefab) as GameObject;
         connectingPanel.transform.SetParent (hudCanvas.transform, false);
 
         //set up single player game
         if ((GameData.currentMode == GameMode.SinglePlayer)) {
             Instantiate (map1Prefab);
             BattleManager.CreateSinglePlayerGame ();
         }
         //set up tutorial
         else if (GameData.currentMode == GameMode.Tutorial) {
             Instantiate (tutorialMapPrefab);
             BattleManager.CreateSinglePlayerGame ();
         }
     }
 }

Comment
Add comment · Show 11
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 tinglers · Mar 11, 2017 at 04:33 PM 1
Share

maybe it's the same object? if you change it in level 1 will this reflect in tutorial?

avatar image ilmix123 tinglers · Mar 11, 2017 at 06:23 PM 0
Share

I tried changing the value of test to 11 in "Level 1" during runtime, then I quit to "$$anonymous$$ain$$anonymous$$enu" scene, and then I started the tutorial. The value of test when starting the tutorial was still 10.

avatar image gjf · Mar 11, 2017 at 04:40 PM 1
Share

are the public variables defined as static?

avatar image ilmix123 gjf · Mar 11, 2017 at 06:04 PM 0
Share

No, they are just public.

avatar image ExtinctSpecie · Mar 11, 2017 at 04:54 PM 1
Share

this shouldn't happen in any way but we can't see the code so it's really hard to give an answer. if you could post some code would help us.

avatar image ilmix123 ExtinctSpecie · Mar 11, 2017 at 06:19 PM 0
Share

I added the code, but there really isn't anything interesting about it.

avatar image Glurth · Mar 11, 2017 at 06:35 PM 2
Share

recommend some sanity checks. alter debug to confirm different objects:

 Debug.Log ("Test in this scene is: " + test + "for object id:" + this.GetInstanceID() + " name:" + this.name);

just to ensure its not an issue with when awake is called..try a debug also in OnEnable()

avatar image ilmix123 Glurth · Mar 17, 2017 at 10:43 AM 0
Share

I just tried that and this.name are both "Loader" (that's kind of expected since both objects have the same name) but this.GetInstanceID() returns different value in each scene.

$$anonymous$$oving Debug.Log() to OnEnable() didn't change anything.

I suspect it might be somehow caused by the fact that I duplicated "Level1" scene to make "Tutorial" scene.

avatar image jwulf · Mar 11, 2017 at 08:21 PM 1
Share

Can you assure that nothing else alters the "test" variable? It is public, after all.

$$anonymous$$aybe run a test making it private (and using [SerializeField] ofc, so you can set the value in the inspector)?

avatar image ilmix123 jwulf · Mar 17, 2017 at 10:52 AM 0
Share

I tried what you suggested, but the result is still the same, test is 10 in both scenes.

Show more comments

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Masterio · Mar 11, 2017 at 09:08 PM

On scene load the object is loaded from current scene so you can do 2 things:

Make static variables or make one object shared between scenes you can use this:

https://docs.unity3d.com/ScriptReference/Object.DontDestroyOnLoad.html

Comment
Add comment · Show 1 · 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 ilmix123 · Mar 17, 2017 at 10:57 AM 0
Share

Yes, but the point is I don't want to share this variable between two scenes. I want it to have 2 different values in two different objects (which is imo the expected behavior). but for some awkward reason it's not working this way.

Btw there is no direct scene transition between those two scenes. Scenes go in order "$$anonymous$$ain$$anonymous$$enu" -> "Tutorial" -> "$$anonymous$$ain$$anonymous$$enu" -> "Level1"

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

105 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

Related Questions

When would I encapsulate variables? 2 Answers

How can i make both two cameras to follow the player but only one with control on player ? 0 Answers

Evaluating animation curves in the Timeline 1 Answer

Seperating editor and runtime data with ScriptableObjects 1 Answer

I can only select a prefab as my GameObject in the editor. 1 Answer


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