0

I'm working on a 2D Unity game, and I want to create a Boom in run time and move it.

Here is part of my code. I create a prefab Boom, and I drop it into inspector.

public GameObject Boom; // prefab Boom is drop here
void OnMouseDown()
{   
    ...
    Vector3 NewBoomPostion = new Vector3 (Luncher.transform.position.x,BoomPosition, 85);
    Instantiate(Boom, NewBoomPostion , Quaternion.identity);
    iTween.MoveTo (Boom, iTween.Hash ("y",BoomendPosition ,"speed",Boomspeed,"EaseType",BoomeaseType,"LoopType",BoomloopType));
}

But it throws this error

NullReferenceException: Object reference not set to an instance of an object iTween.RetrieveArgs ()

rj487
  • 4,476
  • 6
  • 47
  • 88

1 Answers1

2

I think problem is, Instantiate() instantiates a copy of object (Boom). After instantiate it, your new game object doesn't point to Boom object. It's a new game object.

GameObject instantiatedBoom = (GameObject) Instantiate (Boom, newBoomPosition, Quaternion.identity);
iTween.MoveTo( instantiatedBoom,....);

should solve it

Cenkisabi
  • 1,066
  • 8
  • 25
  • 2
    I think you should get reference to new GO like this: GameObject instantiatedBoom = Instantiate (bla bla) as GameObject; instead of casting it to GameObject. – Neven Ignjic Sep 13 '15 at 15:16
  • Your solutions are great, and the Boom move perfectly. However, it shows another error, but it didn't affect anything. I felt wield, so I update my question. – rj487 Sep 13 '15 at 16:20