- Home /
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?
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 "$$anonymous$$ainCamera". If there is no such camera in your scene, it returns null, and then, calling ScreenPointToRay results in the NullReferenceException.
Thanks for taking the time to answer Christian. It was on the yield $$anonymous$$oveObject.use.Translation, line, and I actually figured it out about five $$anonymous$$utes 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?
$$anonymous$$y issue was $$anonymous$$oveObject.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) {
Ins$$anonymous$$d 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. $$anonymous$$y "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 $$anonymous$$oveScript.js attached to another object.
You should post this as an answer to your own question and have someone voting for it.
Answer by stereosound · Sep 16, 2011 at 02:03 AM
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;
}
}
}
Your answer
Follow this Question
Related Questions
Unity iOS Javascript - Problem with Instantiate 1 Answer
iTween NullReferenceException error 1 Answer
Instanciate Object Problem for Shooting 2 Answers
Why am I getting this error ? 2 Answers