x


Pass parameters to an object in another scene

I want to load a new scene (using Application.LoadLevel()) but I would like to pass a parameter to an object in that scene. Is that possible?

Or will I have to write what I need to the PlayerPrefs and then read them on the new scene?

more ▼

asked Feb 08 '10 at 06:44 PM

Dimitris 1 gravatar image

Dimitris 1
150 5 6 11

(comments are locked)
10|3000 characters needed characters left

3 answers: sort voted first

sebas's answer is correct and you have other ways to do this too. you can have a static class called SceneProperties and have a list of properties in it or have a list in it to set it's items. unfortunately none of the methods like LoadLevel and Instantiate don't get additional parameters to send to new objects or all objects in the new scene. so you need to store data in a static class or a GameObject that won't destroy with the scene or put the in prefs. the first method is the best and fastest one and the last one is the slowest and even can fail and is not good for web games. you can easily get exceptions with player prefs so use static classes. unfortunately you can not get the name of current scene and you should use another way to find out what scene you are in. to create static classes and variables use "static" keyword. you don't need to create them and one instance of them will be created at the first frame or even before it and you can set it's properties or add items to it's lists or call it's methods. they are the best solution for creating managers.

more ▼

answered Feb 08 '10 at 09:18 PM

Ashkan_gc gravatar image

Ashkan_gc
9.1k 33 56 117

(comments are locked)
10|3000 characters needed characters left

Maybe don't destroy an object from the old scene by using Don'tDestroyOnLoad and then assigning a variable of that object to something else in your new scene in a Start() or Awake() function? That usually should do the trick.

more ▼

answered Feb 08 '10 at 07:54 PM

Sebas gravatar image

Sebas
4k 12 18 45

(comments are locked)
10|3000 characters needed characters left

You can create an object in first scene, attach a sript to it and sen value of variable in this script. For example if you want to send user login script attachet to game object that will survife after second scene load should be:

// User login:

public var userLogin : String;

function Awake() {

// Do not destroy this game object:

DontDestroyOnLoad(this);

}

Set userLogin in first scene and read in second.

Complete example can be found in my blog http://unity-tutorials.blogspot.com/. Listing 20 and 21.

more ▼

answered Mar 30 at 01:27 PM

marinl gravatar image

marinl
1 1

Given that you are posting the same answer as someone else - perhaps a comment that you have a tutorial would be more appropriate and less confusing for future readers (plus it would be associated with the ticked answer and not appear at the bottom of the list).

Mar 30 at 01:31 PM whydoidoit

BTW the tutorial is nice. If you want to get some exposure we'd be happy to have you cross post quality work like this on Unity Gems which already has a high level of traffic (1-2k visitors per day) and we are always on the look out for good authors to submit relevant work. I'm writing here because I can't comment on your tutorial or find another way of getting in touch.

Mar 30 at 01:36 PM whydoidoit
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

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

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x716
x106

asked: Feb 08 '10 at 06:44 PM

Seen: 7502 times

Last Updated: Mar 31 at 09:16 AM