Works perfectly, I just had another game object that had the script assigned in the scene.
Problem instantiating prefabs from other instantiated prefabs
I'm trying to write a script that has a spawned enemy throw an axe. The enemy (axeKnight) is a prefab that I instatiate with an empty game object. That prefab (axeKnight) has this attack script (EnemyAttack) attached to it.
void RangedAttack ()
{
// Sets the offset of the spawned axe
axeOffset.Set(-xOffset, yOffset, 0f);
// Creates a new game object from the prefab EnemyAxe. I've assigned this in the Unity UI.
GameObject newAxe = Instantiate (EnemyAxe, transform.position + axeOffset, transform.rotation);
// Then apply a velocity to the newly created axe
newAxe.GetComponent<Rigidbody2D>().velocity = new Vector2(-axeXspeed,axeYspeed);
}
When the RangedAttack function is called by Unity, I get this error:
"UnassignedReferenceException: The variable EnemyAxe of EnemyAttack has not been assigned. You probably need to assign the EnemyAxe variable of the EnemyAttack script in the inspector."
I'm dumbfounded, because when I pause the game and inspect the axeKnight clone, I can see that the EnemyAxe is properly assigned to the pre-fab.
The reason I have the script on the axeKnight prefab is it seemed easiest to reference that particular object & instance when its attached to that game object.
Any help is greatly appreciated!
Follow this Question
Related Questions
I get an error message every time i run this script? Any ideas? 0 Answers
My code is instantiating many prefabs, i only want one. 1 Answer
Generating prefabs at origin that are children of moving GameObjects? 2 Answers
How to spawn a prefab at a duplicate objects location 1 Answer
Create individual prefabs 1 Answer