Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 A_C · Jun 26, 2012 at 05:26 PM · javascriptscenescene-loadingmenu

Passing information between scenes?

I am making a racing game. The first scene is the main menu. The user chooses the track, their car, number of opponents and the number of laps, and then the track they would race on is loaded. I am saving that information attached to an empty game object that is set to DontDestroyOnLoad in my second scene, but it seems that the information is not being preserved, or I am not accessing it right.

This is the script attached to my terrain in the Second scene I am trying to load:

 var AICar1 : GameObject;
 var AICar2 : GameObject;
 var AICar3 : GameObject;
 var Car1 : GameObject;
 var Car2 : GameObject;
 var Car3 : GameObject;
 
 var Info : GameObject;
 
 var cam : GameObject;
 var car : String;
 var laps : int;
 var enemies : int;
 
 function pickCar()
 {
     if (car == "Dodge Charger")
     {
     cam.target = Car1;
         Destroy(Car2);
         Destroy(Car3);
     }
     else if (car == "EvoX")
     {
         cam.target = Car2;
         Destroy(Car1);
         Destroy(Car3);
     }
     else if (car == "Catamount")
     {
         cam.target = Car3;
         Destroy(Car1);
         Destroy(Car2);
     }
 }
 function Start()
 {
     DontDestroyOnLoad(GameObject.Find("/RaceInfo"));
     Info = GameObject.Find("/RaceInfo");
     laps = Info.GetComponent("Menu").laps;
     enemies = Info.GetComponent("Menu").enemies;
     car = Info.GetComponent("car").ToString();
         pickCar();
     
 }

Am I doing something wrong here?

Comment
Add comment · Show 1
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 whydoidoit · Jun 26, 2012 at 05:27 PM 0
Share

Add a #pragma strict to the top of the file and tell me what errors you get...

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by whydoidoit · Jun 26, 2012 at 05:30 PM

Add a #pragma strict and I'm sure you will find that laps, enemies etc aren't found. You need to use the none string version of GetComponent.

  Info.GetComponent(Menu).enemies

etc.

Comment
Add comment · Show 6 · 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 A_C · Jun 26, 2012 at 09:28 PM 0
Share

After adding #pragma strict & using the non string version of GetComponent "Info.GetComponent($$anonymous$$enu).enemies" I am getting NullReference Exceptions for all of those variables

avatar image whydoidoit · Jun 26, 2012 at 09:54 PM 0
Share

So this isn't attached to a component that has those script on it I guess - or you can't find the RaceInfo...

Debug.Log both of those things..

avatar image A_C · Jun 27, 2012 at 12:40 AM 0
Share

I dont understand what you mean. what do I need to Debug.Log? what information are you looking for?

Thanks again btw for taking the time to help me :) it seems like you are the only one willing to help lol ;)

avatar image torrente · Jun 27, 2012 at 02:15 AM 0
Share

Hello A_C,

It looks like you are saying that the code above is placed in an empty game object in your second scene. Are you wanting to pass variables from scene 1 (your main menu scene) to scene 2? If so, you need to create an empty game object in scene 1 with the code DontDestroyOnLoad(this); in it. That should preserve a game object from one scene to the next. If you have an object named RaceInfo in your first scene that holds all of the information, make sure the DontDestroyOnLoad(this); is in it.

avatar image A_C · Jun 27, 2012 at 02:18 AM 0
Share

The RaceInfo game object is in the first scene, with DontDestroyOnLoad in its code. Im still having trouble accessing the info I want to preserve

Show more comments
avatar image
1

Answer by Bunny83 · Jun 27, 2012 at 02:45 AM

You want to use DontDestroyOnLoad in the second scene on an object from the first scene? That doesn't make much sense. LoadLevel will destroy all objects before the new one is loaded. If you want to preserve something from a scene, it has to be marked as DontDestroyOnLoad in the old scene. Usually you would do something like:

 DontDestroyOnLoad(gameObject);

Next thing is, if you plan to load the scene multiple times, you get in trouble since the object is preserved from the last time the level was loaded and it's created again when the level is loaded again. So you have two objects (or three or more, depening how often you load the level).

You might want to place such a script in a dedicated start scene. This scene is loaded only once when the game is loaded. This scene contains your data script and just loads your mainmenu scene. This way you can switch between your levels and the mainmenu without problems.

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 imagoculturae · Jun 06, 2014 at 10:29 AM 0
Share

"You might want to place such a script in a dedicated start scene. This scene is loaded only once when the game is loaded. This scene contains your data script and just loads your mainmenu scene. This way you can switch between your levels and the mainmenu without problems."

Could you please explain how to do this? $$anonymous$$any thanks

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

7 People are following this question.

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

Related Questions

SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex); is working badly 1 Answer

How to load (restart) last scene 2 Answers

How do I place a player in the right spot when I change scenes? 1 Answer

SceneManager.GetAllScenes() only returns the current scene 3 Answers

,Enums not being passed between scripts properly 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