Shayke
January 1, 2018, 6:51pm
1
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.
** I assume that code was written “freely” for your post, as otherwise it has some issues. =)
Basen1
January 1, 2018, 7:12pm
3
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!
Shayke
January 2, 2018, 10:45am
4
Basen1:
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.
Shayke
January 2, 2018, 11:07am
6
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.
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.
Shayke
January 2, 2018, 11:19am
8
methos5k:
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.
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.
Shayke
January 3, 2018, 2:39pm
10
methos5k:
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.
Thanks for responding.
Everything Works
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?
Shayke
January 4, 2018, 9:41pm
12
methos5k:
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.
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.