instaitiating object with specifc parameters

Things are going good with my 2d wargame. But I am trying to find out an efficient way to do the following.

How can I instantiate an object and give it parameters?

For example when the player picks the scenario he wants I load the data into my arrays and place the units on the map. I can do this and even set the position. But is the only way to literally create every prefab possible of the different types? Or can I use a generic unit type and when it instantiates it has passed variables that tells it. “I am a German Panzer unit with these statistics”. All I need to pass is the type and country.

Not sure if I understand your issue properly. Your tanks have a script with variables on them that decides whether they’re German and such, right?

If so, you can just go

//make a reference while you instantiate
var Temporary = Instantiate(panzer);

//edit this reference
Temporary.GetComponent(Script).Country = Germany;

You only need to create one prefab.
On that prefab have an object manager. So if it is a tank, call the script TankManager.

In your TankManager class have your public properties…

i.e

public float Health { get; set; }
public float Armour { get; set; }

Then when you instantiate your tank, you can set the properties on the TankManager class from your instantiate script.

i.e

// Instantiate tank from your main GameManager class
var tank = GameObject.Instantiate(_tankPrefab, Vector3.zero, Quaternion.Identity);

// Get TankManager.cs script on instantiated tank
var tankManager = tank.GetComponent<TankManager>();

// Set tank properties
tankManager.Health = 100;
tankManager.Armour = 50;

etc…

That’s exactly what I wanted to know. One of my concerns was instantiating a unit (armor corp) and since it just got instantiated it would immediately run all its scripts. But it seems like the coding will finish what its doing in the current script and THEN go to the new object (armor corp). Since I can initialize the public variables in the preflop in the game manager when the “start” function is called on the object (armor corp) it will have all its public variables filled.

One more question. How do I differentiate different objects of the same prefab?

If I already created the prefab “tank” and now I make a 2nd take how do I differentiate between them when setting variables?

like tank1, tank2, tank3?

What I do is have a GameManager class that basically controls the entire gameplay.
Your GameManager class can store a collection of all tanks that have been spawned in a generic List<>.
This list is responsible for keeping track of the tanks in the scene.

So do something like this…

// Declare your generic list to store the tanks.
List<TankManager> _tanks;


void SpawnTank()
{
    var tank = GameObject.Instantiate(_tankPrefab, Vector3.zero, Quaternion.Identity);
    var tankManager = tank.GetComponent<TankManager>(); 
    tankManager.Health = 100;
    tankManager.Armour = 50;

    // Add spawned tank to your List
    _tanks.Add(tankManager);
}

Then you can access the list of tanks any time, loop though them etc. or do whatever you need to do.
For instance if you wanted to destroy all spawned tanks… you could do this…

void DestroyAllSpawnedTanks()
{
  foreach(var tank in _tanks)
    Destroy(tank.gameObject);
}

Thank you BTW. Your idea helped do what I needed to do.