-2

I'm trying to make decision in my script if I get a value 1 then run specific game object which is (PopulationGenerator of population engine asset). I have 3 population generator in my scene but I can't assign any of them pragmatically in the code to PopulationGenerator variable.

The only way is drag and drop gameobject from side menu on the script in the unity GUI.

This is the code:

public PopulationGenerator generate1;

generat1.totalToSpawn = Convert.ToInt32(number) - 5;

Can i set one of Population Generators in scene like for example

if (value == 1)
    generate1 = gameobject.name("generator1");
else if (value == 2)
    generate1 = gameobject.name("generator2");
...

Is there any method to set a gameobject of any type to a variable in the code ? Thanks.

AustinWBryan
  • 3,249
  • 3
  • 24
  • 42
Mahmoud Elgindy
  • 114
  • 1
  • 11

1 Answers1

1

You can do something like this:

generate1 = GameObject.Find("nameOfGenerator").GetComponent<PopulationGenerator>();
Amin
  • 543
  • 7
  • 12