Shoot.OnGui

This script worked fine up until 2 days ago when I re-installed unity on a new pc.

/* GUI.Button */

var height : float = 200;
var length : float = 50;
var fireballPrefab : Transform;

//Bullit Speed
var power : float = 5000.0;


function OnGUI () {

if (GUI.Button (Rect (Screen.width/2 + length, Screen.height/2 + height, 100, 50), "Fireball")) {
              //Shoots fireball when pressed

var Fireball = Instantiate(fireballPrefab,         GameObject.Find("spawnPoint").transform.position,Quaternion.identity);

Fireball.rigidbody.AddForce(transform.forward * power);

My Concole reports:

NullReferenceException
Shoot.OnGUI () (at Assets/scripts/Shoot.js:13)

What does this mean? I’ve looked every where and I cannot find any information on this.

Looks like a problem with the prefab. Fireballprefab may not have all files referenced inside in same place (path) as old pc.
Hope this helps.

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:

  1. 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.
  2. 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.