Pass on variable values to instantiated GO but check for null... somehow

Hi guys,

I am trying to get the other GO health variable so when it is instantiated I can pass on the current GO health.

The script I use is fine for all my other vehicles, but I dont want to add another script just for this purpose.

I have it like this now for all my vehicles:

Transform newCar = (Transform)Instantiate(spawnControlledCar.transform, transform.position += Vector3.up * 5, transform.rotation);
		
		newCar.GetComponent<VehicleHealthControlledCS>().health = vHealth.health;

and I want to check if that does not exist then find this:

Transform newCar = (Transform)Instantiate(spawnControlledCar.transform, transform.position += Vector3.up * 5, transform.rotation);
		
		newCar.GetComponent<VehicleHealthControlledCS>().health = vHealth.health;
		
		if(newCar == null){
			newCar.GetComponent<VehicleHealthControlledRadarTankCS>().health = vHealth.health;	
		}

but of course throws an error.

Any tips ideas?

Thanks.

Why you instantiate Transform?

GameObject newCard = Instantiate(...) as GameObject;

Object.Instantiate can be used to instantiate component in which case the parent GameObject is cloned:

EDIT:

You do:

newCar.GetComponent<VehicleHealthControlledCS>().health = vHealth.health;
if(newCar == null){

That doesn’t make sense. No point checking if newCar is null because you already reference it as if it wasn’t null in previous line. If it is null then you have already generated an error before the if(newCar == null) is executed. You should do instead:

if(newCar != null)
{
   newCar.GetComponent<VehicleHealthControlledCS>().health = vHealth.health;

What is the the thrown error?

I used Transform cause I had an error previously, after researching I found using Transform worked.

Any tips on my question?

Yes very close, but new object has different scripts for the user to control the vehicle, I just need to pass on its health value and I don’t really want to create another script.

So, you need to verify that you’ve actually found the script in question prior to attempting to set one of its members. Untested, but that’d look something like this:

VehicleHealthControlledCS hc1 = newCar.GetComponent<VehicleHealthControlledCS>();
if (hc1)
{
   hc1.health = vHealth.health;
}
else
{
   VehicleHealthControlledRadarTankCS hc2 = newCar.GetComponent<VehicleHealthControlledRadarTankCS>();
   if (hc2)
   {
      hc2.health = vHealth.health;
   }
}

What you mean “very close”? I’m not taking a guess. I’m stating a fact - look documentation.
What do you mean: new object has different scripts? No it doesn’t. Object.Instantiate clones the object so the new object has exactly the same scripts that original object has.

Thanks jgodfrey, I will give it a try.

I mean very close to my needs, I do not want to make a clone.

OK. So you can either create a new GameObject:

GameObject newOne = new GameObject();

Then use:

to add all components.

If you don’t want to build the object from scratch because the one you need is very close to one you laredy have then you can use:

to clone th existing object and use:

to remove unwanted components like so:

Destroy(clone.GetComponent<UnwantedComponent>());

and use AddComponent to add all components that clone is lacking.

Yep it worked perfect.