Changing variables of spawned prefabs

Hi guys,

How would i change the instances of spawned prefabs?

like if im spawning 10 soldiers, how can i make them run at different speeds?

Thanks

I like this pattern:

Factory Pattern in lieu of AddComponent (for timing and dependency correctness):

Keeps things tidy and centralized, yet lets you customize whatever you want.

1 Like

Wow thanks Kurt, will give it a go!

1 Like

If you’re talking about setting random speeds, you can just generate the random speed in an Awake or Start method on the prefab. When the prefab is instantiated, that method will run, and set the speed for just that instance.

public float MySpeed;

void Awake()
{
   MySpeed = Random.Range(3f, 10f);
}

Another way is to have the script which instantiates the prefab set these kinds of things.

GameObject newSoldier = Instantiate(SoldierPrefab);
newSolder.GetComponent<SoldierStats>().MySpeed = Random.Range(3f, 10f);
1 Like

Yes that makes sense Joe thanks. I will do it the simple way and when i have enough done i am going to return to different sections and try implement more elaborate solutions. I’m a very bad programmer you see…

1 Like

Inexperienced and bad aren’t the same thing :slight_smile:

3 Likes