A null reference exception happens when you try to reference an object which doesn’t exist! In your script it’s probably happening because the ‘GameObject.Find(“whatever”)’ bit isn’t returning anything (because the object in question isn’t there). Is there any reason why you can’t assign the spawn-point inside the editor? If you make the spawn point a Transform which you set up in the editor, then you can be sure it won’t have any problems there.
The exception happens in line 13. It’s hard to spot that line in such a post but you should be able to find the line in question. Anyway it can only be the Instantiate line. Two possible errors can occur:
Your fireballPrefab is null because you forget to assign your prefab in the inspector. In some cases Unity can loose the reference when you change the variable name.
The “spawnPoint” gameobject doesn’t exist (or doesn’t exist anymore).
Usually you should check those “User inputs” if they hold valid data
var Fireball = Instantiate(fireballPrefab, GameObject.Find(“spawnPoint”).transform.position,Quaternion.identity);
if (GUI.Button (Rect (Screen.width/2 + length, Screen.height/2 + height, 100, 50), "Fireball"))
{
var spawnPoint = GameObject.Find("spawnPoint");
if (spawnPoint == null)
{
Debug.LogWarning("Tried to spawn a Fireball, but no Spawnpoint found!");
return; // Quit here
}
if (fireballPrefab == null)
{
Debug.LogWarning("Tried to spawn a Fireball, but the prefab isn't assigned");
return; // Quit here
}
// At this point we know for sure that we have a fireball prefab and a spawnpoint
var Fireball = Instantiate(fireballPrefab,spawnPoint.transform.position,spawnPoint.transform.rotation);
}
The prefab check can or should be done in Awake() and if it fails it should just disable the script so it doesn’t produce an endless chain of exceptions. Just make sure to inform the User. It doesn’t help debugging when you catch an error and ignore it.