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. :slight_smile:

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 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:

http://unity3d.com/learn/tutorials/modules/beginner/live-training-archive/persistence-data-saving-loading