Question by
MilesAdamson · Oct 23, 2015 at 11:57 AM ·
instantiatereference
Instantiate leading to reference not set error
I am trying to instantiate a "shot" prefab which is of class shot. I am getting the error, "object reference not set to an instance of an object"
Vector2 pos = new Vector2(mybody.transform.position.x + direction, mybody.transform.position.y);
Shot newshot = Instantiate(missile, pos, Quaternion.identity) as Shot;
newshot.GetComponent<Rigidbody2D>().AddForce(new Vector2(3 * direction, 1), ForceMode2D.Impulse);
newshot.setatk(magic_atk);
Debug.Break();
The nexshot.GetComponent line doesn't work and i have no idea what I'm doing wrong. The shot is successfully made, I click on it and it has the shot scripted attached, but this line makes it bug out and never successfully gets put through. What happens is it keeps making shots indefinitely.
The shot class has a public rigidbody2d and int magic_atk
Comment