Instantiate with vars.

Hi , i am trying to summon an object with parameters
I have this class:

public class CoinClass

int Money;

this is my command:

public gameObject Coin;

GameObject tmp = (GameObject)Instantiate(Coin, transform.position, Quaternion.identity);

I want Instantiate the coin with Money, for example Money = 15;
Any ideas?

Sure, if it’s always the same put it in Awake().

If it’s dynamic, then add a property or method that you can set or call right after you instantiate your game object. :slight_smile:

** I assume that code was written “freely” for your post, as otherwise it has some issues. =)

Hey, just make the int Money public.

public class CoinClass() // The script on the coin
{
public int Money; // Can also give a default value here
}

And then set it once you instantiate set its value like so

public GameObject Coin;
GameObject tmp = (GameObject) Instantiate(Coin, transform.position, Quaternion.identity);
tmp.GetComponent<CoinClass>().Money = 15; // some value

Hope this helps. GL!

I have another issue, my prefab needs a component.
How do i set it for him?

If you want it on every prefab, add it to the prefab. If you only want it on some, then do: AddComponent() after you instantiate it. :slight_smile:

sorry i think i did not explain my self like i wanted.
My prefab(Enemy) needs to know who is the target.
When i instantiate him, its gone i dont know why.

3339087--260624--unity problem.png

You cannot store a reference to a scene object in a prefab is the reason why.

You need to “get” the reference when it’s made.
I would suggest that you store a reference to the player on the spawner script. Then ,when you instantiate your enemy, pass the variable to the enemy’s script. :slight_smile:

And how can i do it?
lol, sorry i am new to Unity.

Well, you have a script on enemy, right? And you have a spawning script, yes?
So, it would look something like this:

// on spawning script
[SerializeField]GameObject player; // drag n drop in the inspector

// now, when you spawn
GameObject newEnemy = Instantiate(EnemyPrefab);
newEnemy.GetComponent<EnemyScript>().playerTarget = player;


// Now, on the Enemy Script:
public GameObject playerTarget;

That’s it. Try something like that. :slight_smile:

Thanks for responding.
Everything Works :slight_smile:
Btw

    public void Summon()
    {
        GameObject tmp = (GameObject)Instantiate(Enemy, Position.transform.position, Quaternion.identity) as GameObject;
        tmp.GetComponent<NewEnemy>().Target = Target;
        tmp.GetComponent<NewEnemy>().Cam = Cam;
        tmp.GetComponent<NewEnemy>().Drops = Drops;
    }

How can i get the prefab’s Details without really instantiate

details without instantiating… hm… do you mean some values on a script on the prefab or something?
For that, you can actually use “GetComponent” on the prefab game object.
Is that you meant?

I need your help, I deleted my object and now i have him as a prefab.
How can i get him to his last state?

Sorry, can you explain the question a bit better? Feel free to make a new thread if it’s a new question.