Instantiate code crashing in Unity 3 but not Unity 2?

Hi, I have some c# code which instatiates a bullet when you press fire:

    public static void InstantiateBullet( int source, Vector3 position )
    { 
        // Put this in awake    
        Object myBullet = Resources.Load("Bullet");
        Vector3 temprotation = Vector3.zero;
  
        GameObject bullet = GameObject.Instantiate(myBullet, position , Quaternion.AngleAxis(90, Vector3.left) ) as GameObject;
        if ( source == 0 )
        {
            bullet.tag="PlayerBullet";
        }
        else
        {
            bullet.tag = "EnemyBullet";
        }

        NewBullet refenceToBulletComponent = bullet.GetComponent<NewBullet>();
        refenceToBulletComponent.bulletOrigin = source;
     }

Under Unity 2 this works fine - you press a button and this bullet is instantiated, however now it seems to just crash the editor - the game is automatically paused but with no specific error in the console and I have to quit Unity and restart it - can anyone help me out? BTW I know this code is slow - not interested in the optimisations just now, just getting it working again!

Thanks very much for any assistance

I have a simular problem,
Any where any static functions or varibles are used this error is caused.
“Object reference not set to an instance of an object.”

I’m seeing the same question posted over and over with out an answer. If there is a fix will some one please make it a sticky topic or post it in the unity ansers. I would think that by now the answer is posted some where.

DarrinT: An “Object reference not set ot an instance of an object” is caused by you trying to do something to a null object. That is, there is likely a logical bug in your code, not an issue with Unity 3 vs 2.

var myString : string = null;
Debug.Log(myString.Trim()); //causes the error message; how can you "trim" a null object?

bishylewis: the best I can guess is you keep running Resources.Load(“Bullet”) each time. Instead try running that once, storing that object, and keep reusing it.

@FizixMan: no somthing has changed between verson 2.61 and 3.0.0F5
now I might be doing somthing that is not the best coading method but the code I had did work in version 2
My code is in java script

In my scene I have an empty object named Gui with a Script attached to it called My_GUI.js where I build the buttons for the user interface
The interface loads properly.

My_GUI has this variable
static var btnOrbitToggleState: boolean = false;

On my camera for the scene
I have a script attached to the camera object with a function that needs to check the varible in the GUI script

function LateUpdate () {

var GUIobject = GameObject.Find(“Gui”);
// appears to create an instance of the object

var GUIcode = gobject.GetComponent(“My_GUI”);
// appears to create an instance of the objects code

var checkOrbitCamera = GUIcode.btnOrbitToggleState;
// this gives the NullReferenceException: Object reference not set to an instance of an object

Again this use to work for over a year till updated to unity 3
In unity 2, I had to declare the varible as staic inorder to see it from another object. I also had to declare a function as static inorder to get an instance to it from another function.

Shouldn’t your line:
var GUIcode = gobject.GetComponent(“My_GUI”);

be:
var GUIcode = GUIobject.GetComponent(“My_GUI”);

The only way you can get this error, from where you’re saying it’s being thrown, is if “GUIcode” is null. The only way that can be null is if “gobject” doesn’t have a “My_GUI” script attached.

sorry miss coppied I hve two simular section in my code.

But I just figured out what I need to chenge it to.
var GUIcode:My_GUI = GUIobject.GetComponent(“My_GUI”);

by changing it to var GUIcode:My_GUI I now have access to the static varibles and static function in the Component.

Thanks

You understand that to access static variables and methods you should not use an instance (if you even can).

var GUIcode:My_GUI = GUIobject.GetComponent(“My_GUI”);
GUIcode.MyStaticMethod(); //wrong

My_GUI.MyStaticMethod(); //correct

Fizixman - cheers, so I declared:

private static Object myBullet;

And put this in start():

NewBullet.myBullet = Resources.Load("Bullet");

…but now when I run it it says

“ArgumentException The prefab you want to instantiate is null.”

I’ve no idea why this would change between Unity 2 and 3 :rage:

Any ideas appreciated