x


NullReferenceException with MoveObject.js

I get the NullReferenceException when trying to use the MoveObject.js script here: http://www.unifycommunity.com/wiki/index.php?title=MoveObject

Basically, I instantiate 5 objects (object0, object1, etc.) then creating a "zoom" script with the following code; unfortunately, I can't get it to move.

function Start()
{
    StartCoroutine("CoStart");
}

function CoStart() : IEnumerator
{
    while (true)
        yield CoUpdate();
}

function CoUpdate() : IEnumerator
{
    var hit : RaycastHit;
    var itsaray : Ray = Camera.main.ScreenPointToRay(Input.mousePosition);
    Debug.DrawRay (itsaray.origin, itsaray.direction * 200, Color.yellow);
    if (Physics.Raycast (itsaray, hit) && Input.GetMouseButtonDown(0) && !isMoving) {  
       if (name == hit.collider.gameObject.name) {
         var transform2 : Transform = hit.collider.GetComponent(Transform);
         yield MoveObject.use.Translation(transform, Vector3(15.5, 26.75, 15), 1, MoveType.Speed);
          }
    }
}

I'm starting to think it's an issue with the MoveObject.js script that cannot take the transform reference. Is this the case?

more ▼

asked Sep 15 '11 at 04:32 AM

stereosound gravatar image

stereosound
33 5 8 9

It's called an "Exception". Not an "Expection". ;-) Can you take a look at the error that appears in the editor's console to see which line of code causes the exception? The line number appears in the stacktrace that comes with the error; use that to find the line, then highlight that for us. It makes it easier to help you.

As a quick first guess, it might be because you don't have a main camera. Notice that you code accesses Camera.main. This property returns the first camera it finds tagged "MainCamera". If there is no such camera in your scene, it returns null, and then, calling ScreenPointToRay results in the NullReferenceException.

Sep 15 '11 at 08:24 AM CHPedersen

Thanks for taking the time to answer Christian. It was on the yield MoveObject.use.Translation, line, and I actually figured it out about five minutes after I posted this. Funny how you can have a problem for over a day, then the moment you ask for help it dawns on you?

My issue was MoveObject.js wasn't attached to an object; the object it was attached to got accidentally deleted as a result of bad coding that I tried to troubleshoot before. I was trying to do:

var transform : Transform = hit.collider.GetComponent(Transform); if (transform) {

Instead of just:

   if (name == hit.collider.gameObject.name) {

Turns out in trying to bugfix this I deleted my object, then when I came up with a better method it still didn't work as a result.

At the very least, perhaps this will give someone an idea of how to zoom-in on instantiated objects. My "create" code is something like this:

function Start () { for(var i = 0; i < 5; i++) var object = Instantiate (prefab, Vector3(i * 2.0, 0, 0), Quaternion.identity); object.name = "Object" & i } }

The prefab has the above script attached, with MoveScript.js attached to another object.

Sep 15 '11 at 11:43 AM stereosound

You should post this as an answer to your own question and have someone voting for it.

Sep 15 '11 at 12:50 PM ericksson

Agreed - Good you got it fixed!

Sep 15 '11 at 12:58 PM CHPedersen
(comments are locked)
10|3000 characters needed characters left

1 answer: sort voted first

I fixed the problem; through some bad debugging I accidentally deleted the object that MoveObject.js was attached to, thus the script couldn't be called. Adding it to any object (mine is on an empty gameobject) will make this script work.

For reference for anyone who is trying to create a zoom-in on cards: ATTACH TO EMPTY GAMEOBJET:

function Start () {
    var card_arr : Array = new Array();
       for (var z = 1; z < 7; z++) {
           // 7 is arbitrary deck size + 1
       card_arr.Add(z);
       }
       deckSize = card_arr.length;
       for (var x = 0; x < decksize; x++) {
        var card_made = Instantiate(card_prefab, Vector3 (x*0.7, 0, 0), Quaternion.identity);
        card_made.transform.Rotate(Vector3 (0, 180, 0));
    //rotated because the material applies upsidedown
        card_made.name = "object"+x;
        card_made.tag = "object"+x;
        var choice = Random.Range(0,card_arr.length);
    // this changes the picture of the card to the correct reference
        card_made.renderer.material.mainTextureOffset = Vector2 (card_arr[choice]*0.1, 0);
        card_arr.RemoveAt(choice);
    }
}

This is then attached to the prefab:

function Start()
{
    StartCoroutine("CoStart");
    //semi-cheating, this is just the position on camera that a normal sized object will take up the full screen, playing with perspective rather than scaling
    endPos = Vector3(15.5,26.75,15);
}

function CoStart() : IEnumerator
{
    while (true)
        yield CoUpdate();
}

function CoUpdate() : IEnumerator
{
    var hit : RaycastHit;
    var itsaray : Ray = Camera.main.ScreenPointToRay(Input.mousePosition);
    if (Physics.Raycast (itsaray, hit) && Input.GetMouseButtonDown(0)) {  
       if (name == hit.collider.gameObject.name && Card_Create.CARDZOOMED == zoomed) {
                 //There is probably a more sophisticated way to toggle booleans
         if (Card_Create.CARDZOOMED) {
          Card_Create.CARDZOOMED = false;
         }
         else {
          Card_Create.CARDZOOMED = true;
         }
         if (zoomed) {
          zoomed = false;
         }
         else {
          zoomed = true;
         }
         startPos = transform.position;
         yield MoveObject.use.Translation(transform, transform.position, endPos, 125, MoveType.Speed);
                    //Switches startPos and endPos
         transPos = endPos;
         endPos = startPos;
         startPos = transPos;
       }
    }
}
more ▼

answered Sep 16 '11 at 02:03 AM

stereosound gravatar image

stereosound
33 5 8 9

(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x3460
x1673
x396

asked: Sep 15 '11 at 04:32 AM

Seen: 607 times

Last Updated: Sep 16 '11 at 02:03 AM