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 EoinTrainor11 · Dec 04, 2017 at 10:03 AM · if-statementsscene-switchingscenes

Unity scene IF statement not working?

I'm using an IF statement that checks the current scene to see if I instantiated objects in them previously and if so recreate them in the same place when I return to that scene. However I'm having some grief with getting IF statement for "scene2". Both scenes are have identical scripts in it that use SceneManager.GetActiveScene() and can recognise what scene is active ok, yet still the IF statement isn't working because of the scene part of it...

  void Start() {

     /*Debug.Log(data.scene2_spheres.Count);
     Debug.Log(scenes_script.scene_Name);*/
     
     //This will check if spheres were created in the scene previously before going onto it
     //if (scenes_script.scene_Name == "scene1" && data.num_of_spheres1 >= 1)


     //THIS WORKS FINE

     if (scenes_script.scene_Name == "scene1" && data.scene1_spheres.Count >= 1)
     {
         Debug.Log("Is bigger than 1");
         //this will add up until it makes the list total of the spheres
         for (int i = 0; i < data.scene1_spheres.Count; i++)
         {
         
             //Instantiate(data.scene1_spheres[i], data.scene1_vecs[i], trans.rotation);
             Instantiate(prefab, data.scene1_vecs[i], trans.rotation);

         }

     }


     //Same for scene2...

     //FOR SOME REASON IT DOESN'T RECOGNISE SCENE2 IS ACTIVE...


     if (scenes_script.scene_Name == "scene2" && data.scene2_spheres.Count >= 1)
     {
         Debug.Log("Is bigger than 1");

         for (int i = 0; i < data.scene2_spheres.Count; i++)
         {
             Instantiate(prefab, data.scene2_vecs[i], trans.rotation);
         }
     }

 }


Haven't done much scripting with scenes but a bit stumped here.

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 andyborrell · Dec 04, 2017 at 10:53 AM 0
Share

The problem must be in where scenes_script.scene_Name gets set. Could you post some of that script?

avatar image EoinTrainor11 andyborrell · Dec 04, 2017 at 11:22 AM 0
Share

Sure.


 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using UnityEngine.Scene$$anonymous$$anagement;
 
 public class scenes : $$anonymous$$onoBehaviour {
 
     public Scene Scene;
 
     public string scene_Name;
 
     // Use this for initialization
     void Start () {
 
         Scene = Scene$$anonymous$$anager.GetActiveScene();
 
         scene_Name = Scene.name;
 
        // Debug.Log(data.scene1_spheres[0]);
 
     }
     
     // Update is called once per frame
     void Update () {
 
        
 
         if (Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.C) && scene_Name == "scene1")
         {
             //Debug.Log("scene one to scene two");
             Scene$$anonymous$$anager.LoadScene("scene2");
 
             //Debug.Log("The first vector in the list is now" + data.scene1_vecs[0]);
 
             
 
         }
 
         if (Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.C) && scene_Name == "scene2")
         {
            // Debug.Log("scene two to scene one");
             Scene$$anonymous$$anager.LoadScene("scene1");
 
             
         }
 
     }
 }
 


1 Reply

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

Answer by andyborrell · Dec 04, 2017 at 11:44 AM

This is probably a script execution order issue. When you load the new scene you are relying on the two Start functions happening in the right order. For your code to work you need things to happen in this order:

  • scenes.Start() // This sets the value of scene_Name

  • OtherClass.Start() // This uses the value of scene_Name (I don't know what the class in the original question is called)

    By default you can't rely on the Start functions happening in the right order. However Unity does allow you to define an order. See https://docs.unity3d.com/550/Documentation/Manual/class-ScriptExecution.html

    In your case you want to add the scenes class to the custom order list and give it a negative value to ensure that it runs first.

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 andyborrell · Dec 04, 2017 at 11:50 AM 0
Share

If you were using DontDestroyOnLoad on the scenes object then you have a different problem, which is that Start isn't called when a new scene is loaded for that object. If that's the case you need to use OnLevelWasLoaded to update scene_Name as well as Start.

avatar image EoinTrainor11 · Dec 04, 2017 at 11:58 AM 0
Share

You are a champion sir, never encountered this sorta problem before. Thanks a million.

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

72 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

Related Questions

DontDestroyOnLoad( ) Issue with Instantiation 2 Answers

How can I change a scene by the rotation z position?, 1 Answer

Problem when switching between scenes 0 Answers

How can I make a loading screen with LoadSceneAsync 2 Answers

Bolt Visual scripting and loading different scenes/levels 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