- Home /
Passing a variable to Start() - C#
I have a scene that is setup to battle an opponent. The first time it's called I use a default enemy, due to story line, but when I call the same scene later, I'd like to get a new enemy. So, I overloaded the Start() with Start(int e). Which would allow me to send an enemy id to start the scene.
The problem is, I don't know how to pass a variable to the Start() method. So, I'm guessing that I need to create a script that adds the id to a variable in a GameObject before the scene is called, and then use the Start() method to get and set?
I'm pretty new to programming, so that's probably why I can't figure this out. Basically, I know that there is always a better way to do things that I do. :)
A very simple and 'clean' way would be to make an Init
method that takes whatever parameters you need, and then just call it from Start
- and that other place.
public void Init(int x, etc)
{
// initialize stuff...
}
void Start()
{
Init(...);
}
// somewhere else...
meInstance.Init(...);
No need for hacks...
shouldn't the object be non destroy-able one for such patter to work?
since he has to reload the whole scene again, is singleton like this helpful?! Sorry if i am missing something?!
? - who mentioned singletons? :) - maybe you got confused with meInstance? - I just meant an instance of whatever class he has... no singletons involved.
$$anonymous$$aybe if he could give us more information, like who wants to call this Start, we could give him better ideas... Cause he might have tried doing what I said (he mentioned overloading Start...)
yea.. also, if he wants to set the information from another scene, he could maybe use a ScriptableObject
to store the settings? and so then on his Start method, he could read the enemy ID that corresponds to the current level from that ScriptableObject (of course it has to be saved as an asset) - and then he could change the settings from any level. I think this is a more easy to maintain way...
Another way would be to have a LevelSettings$$anonymous$$anager
that has DontDestroyOnLoad
on it - and have his levels read the ids from it...
Answer by flamy · Mar 18, 2014 at 05:36 AM
In case the start Has to be called from Another scene
Sending parameter to start is not a good solution and it would take a lot to make it work ... instead have a PlayerPref to exchange data from a scene to another
in Start() check this
int e = 0;
void Start()
{
e = PlayerPref.GetInt("EnemyValue",0); ///assuming first time the player pref is not set.
}
void OnApplicationQuit()
{
PlayerPref.DeleteKey("EnemyValue");
}
In the other script set the value of the playerpref to the value you want. Do it at the place where you intend to call the Start with param!. and load level
PlayerPref.SetInt("EnemyValue",5);
Application.LoadLevel(0); /// just an example
In Case start needs to be called from the same scene (the below approach is done by @vexe)
A very simple and 'clean' way would be to make an Init method that takes whatever parameters you need, and then just call it from Start - and that other place.
public void Init(int x, etc)
{
// initialize stuff...
}
void Start()
{
Init(...);
}
// somewhere else...
meInstance.Init(...);
I've been using PlayerPrefs this whole time, and didn't think of that, lol. Thanks flamy
Answer by haim96 · Mar 18, 2014 at 11:41 AM
i know this is closed question but i would like to point you to a better way to keep data between scenes without using playerpref. the part on DontDestroyOnLoad is relevent for you:
Your answer
![](https://koobas.hobune.stream/wayback/20220613140528im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
How to reference a public gameobjects in script which are attached in the inspector. (solved) 2 Answers
Distribute terrain in zones 3 Answers
C# Check If Gameobject is within Collider 1 Answer
How to store player model and then swap the model to an object I Raycast hit? 0 Answers