- Home /
I am trying to instantiate a new enemy into the scene, then change the state of that enemy by calling to a function in its script, any critique on how I could go about it or fix it? Am I missing something?
GameObject newEnemy = Instantiate (enemy, spawnPoints[i].position, spawnPoints[i].rotation);
newEnemy.gameObject.GetComponent ().attackNOW();
Answer by fafase · Oct 20, 2017 at 07:37 AM
First you need to cast the Instantiate as it returns Object. With the GameObject reference you can use GetComponent with the component type. That would be the class holding the AttackNOW method.
GameObject newEnemy = (GameObject)Instantiate (enemy, spawnPoints[i].position, spawnPoints[i].rotation);
newEnemy.GetComponent<ScriptWithAttackNOWMethod> ().attackNOW();
Answer by unit_nick · Oct 20, 2017 at 04:39 AM
Assuming the first line works as intended
GameObject newEnemy = Instantiate (enemy, spawnPoints[i].position, spawnPoints[i].rotation);
then to call a function on that object you simply call the function
newEnemy.attackNOW();
@unit_nick what you say would only be possible if GameObject had an attackNOW() method, which it doesn't. Hence the need for GetComponent ("Custom Type").
true. I missed that because I didn't really look at it. so yeah you would make sure newEnemy is created as the correct type of object
Enemy newEnemy = (Enemy) Instantiate (enemy, spawnPoints[i].position, spawnPoints[i].rotation);
Answer by Harinezumi · Oct 20, 2017 at 08:47 AM
(This is more a comment to @fafase's answer, but I wanted it to have nice formatting)
Actually, you don't need to cast to GameObject, Instantiate will return a type of the first parameter. Because of this if I only need that type, what I usually do:
[SerializeField]
private Enemy enemyPrefab = null; // set reference from Editor
// within the enemy spawning function
Enemy newEnemy = Instantiate(enemyPrefab, spawnPoints[i].position, spawnPoints[i].rotation); // because enemyPrefab has type Enemy, you can directly assign without casting
newEnemy.attackNOW(); // note that this will execute after Enemy.Awake(), but before Enemy.Start()
If for any reason you need to work with other components on newEnemy, then of course you need GetComponent().
For the generic version it does. The docs is quite clear about it, Object.Instantiate returns Object.
https://docs.unity3d.com/ScriptReference/Object.Instantiate.html
Yeah I wasn't sure if I should comment on this or not. From the way it is worded it sounds like it is something @Harinezumi does do and that it works. But nowhere can I find it documented. So although it may work should you do it? This is the sort of thing that breaks with updates.
It's because I don't have a reference to GameObject enemyPrefab, I use directly a reference to an Enemy script (if the prefab has the script, you can assign it), so when I call Instantiate(enemyPrefab, ...) it is not using GameObject.Instantiate(Object ...), it can infer that from the type of enemyPrefab that I want to use GameObject.Instantiate(...). Here is an answer that explains it.
The script reference does very basic explanations in certain cases, but in practice there are a lot of completely legal shortcuts (it is slightly faster if you avoid the cast).
Your answer
Follow this Question
Related Questions
Why is it important to create an empty gameobject for my prefabs? 0 Answers
How to add gameobjects to opened prefab with EditorWIndow? 0 Answers
Instantiate prefabs next to each other? 1 Answer
prefab not loading with instantiate 2 Answers
How to change Image on button click in a prefab that is instantiated 1 Answer