- Home /
Instantiate Script Not Working After Update
Greetings,
I recently updated my unity to 2018.3.0b5 and attempted to update my project to this version as well.
There have been, of course, a few errors in the game, most of which I have been able to fix. However, one still remains and I'm not quite sure what to do.
The script I made to drop items (health and mana potions) no longer functions at all, but the compiler doesn't detect any errors.
Unity gives this message:
ArgumentException: The Object you want to instantiate is null. UnityEngine.Object.CheckNullArgument (System.Object arg, System.String message) (at C:/buildslave/unity/build/Runtime/Export/UnityEngineObject.bindings.cs:383) UnityEngine.Object.Instantiate (UnityEngine.Object original, UnityEngine.Transform parent, Boolean instantiateInWorldSpace) (at C:/buildslave/unity/build/Runtime/Export/UnityEngineObject.bindings.cs:262) UnityEngine.Object.Instantiate[GameObject] (UnityEngine.GameObject original, UnityEngine.Transform parent, Boolean worldPositionStays) (at C:/buildslave/unity/build/Runtime/Export/UnityEngineObject.bindings.cs:300) UnityEngine.Object.Instantiate[GameObject] (UnityEngine.GameObject original, UnityEngine.Transform parent) (at C:/buildslave/unity/build/Runtime/Export/UnityEngineObject.bindings.cs:295) ItemDrop.DropItem () (at Assets/Scripts/Enemy/ItemDrop.cs:35) EnemyHealth.TakeDamage (Int32 amount) (at Assets/Scripts/Enemy/EnemyHealth.cs:90) FireballController.OnTriggerEnter2D (UnityEngine.Collider2D other) (at Assets/Scripts/Player/FireballController.cs:46)
I think the script that is not working is this one but I am not sure:
public class ItemDrop : MonoBehaviour
{
public GameObject[] drops;
private Rigidbody2D rb2d;
public int spawnChanceThreshold = 7;
void Start()
{
rb2d = GetComponent<Rigidbody2D>();
}
public void DropItem()
{
int itemSpawnChance = Random.Range(1, 10);
if ((itemSpawnChance >= 1) && (itemSpawnChance < spawnChanceThreshold))
{
return;
}
else if (itemSpawnChance >= spawnChanceThreshold)
{
int itemToSpawn = Mathf.RoundToInt(Random.Range(0, drops.Length));
Instantiate(drops[itemToSpawn], rb2d.transform.position, Quaternion.Euler(new Vector3(0, 0, 0)));
}
}
}
Can anyone please point me in the right direction to fix this? I would be happy to share any code if additional information is needed.
Please note that this is my first game in Unity and my second time posting on the forums. I am aware that I have no idea what I'm doing, so helpful comments only please :)
Answer by austinsun102 · Jan 02, 2019 at 11:15 PM
Check the drops variable is not null in the hierarchy. If that’s not the problem then I don’t really know what it is. The argument basically means that the game object drops is null and when you try to instantiate drops, it can’t instant drops because it’s null.
Also: a little tip is instead of using quaternion.euler(new vector3 (0, 0, 0)), you can using Quaternion.identity and also if you ever want to use a vector 3 with a value of 0,0,0, you can use vector3.zero
Answer by seeker727 · Jan 02, 2019 at 11:45 PM
Thank you for your input!
Apparently after instantiating the prefab enemy, the drops field (which is filled when the enemy is a prefab) becomes null when the enemy is in the hierarchy.
I was able to fix the issue by deleting the prefab, remaking it and re-filling the fields, and re-saving it as a new prefab.
Now I just have to do that with every single enemy prefab . . . . lol.
And thank you for the info about Quaternion.identity and vector3.zero. I will definitely use those going forward!