- Home /
ArgumentException: The prefab you want to instantiate is null.
I am rather new to Unity and C#, and need help. I have looked around on the forums for people with similar problems but have yet to find anything that helps.
There are two game objects that need to be instantiated a wolf-object and a rabbit-object. I have scripted them in two different ways which seem the same to me. Why is does instantiating the wolf-object throw the above error and how can I correct this? In both cases the prefab has been assigned to their relevant scripts.
using UnityEngine;
using System;
using System.Collections;
using System.IO;
using System.Linq;
public class SpeciesCreation : MonoBehaviour {
public GameObject RabbitObj;
int RabbitInit=30;
WolfProps p=new WolfProps();
void Start ()
{
p.WolfCreate();
for(int i=1;i<=RabbitInit;i++)
{
var RabbitObject = GameObject.Instantiate(RabbitObj, new Vector3((float)(UnityEngine.Random.value-0.5)*90f+50f,100.25f,(float)(UnityEngine.Random.value-0.5)*90f+50f),Quaternion.identity);
}
}
void Update ()
{
EquilibriumMathEngine.EquilibriumCalc();
}
}
using UnityEngine;
using System.Collections;
public class WolfProps : MonoBehaviour
{
public GameObject WolfObj;
public int WolfCreate()
{
{
var WolfObject = GameObject.Instantiate(WolfObj, new Vector3((float)(UnityEngine.Random.value-0.5)*90f+50f,101.25f,(float)(UnityEngine.Random.value-0.5)*90f+50f),Quaternion.identity);
return 1;
}
}
}
Thank you in advance for any help :)
I tried your script, and it worked fine. I tried a couple of things to produce errors, and was unable to produce the error you have above. A couple of things:
Attach a different prefab to the WolfObj. If it works, you know the issue is likely in the prefab and you'll want to exa$$anonymous$$e any scripts attached to the objects in the prefab.
If you still get error, check to make sure you don't have the the Wolf script attached to multiple objects.
Answer by RoloJo · Feb 26, 2013 at 06:48 PM
Thanks for the reply.
I have dragged the prefab representing the Wolf-object into script and tried that. Then I dragged the script over onto the wolf prefab. Neither worked.
If I made the mistake that I think you are speaking of would it not give the following error: "UnassignedReferenceException: The variable has not been assigned."
Your answer
Follow this Question
Related Questions
Can't instantiate bullets when calling shoot function in one script from another script. 1 Answer
Some Prefabs are not Loading 1 Answer
ArgumentException: The prefab you want to instantiate is null. 1 Answer
prefab instantiated by script containing ShadowCaster2D bugs out 0 Answers
Instantiate Object At Local Position 2 Answers