Hey guys: I’m trying to base the direction of my projectile based off the direction of my player, but when I load it from resources it shows me a null reference, which is odd to me.
This is my code as it stands, and I am confused as to what I’m missing. I’m relatively noobish, so I know this may be an easy fix, but any help would be appreciated!
public class BulletScript : MonoBehaviour {
GameObject sphere = GameObject.Find("Player");
float speed = .5f;
Vector3 direction;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
//launch bullet
if (sphere.GetComponent().movedDown)
{
direction = Vector3.forward;
Vector3 velocity = speed * direction;
transform.position -= velocity;
}
if (sphere.GetComponent().movedUp)
{
direction = Vector3.forward;
Vector3 velocity = speed * direction;
transform.position += velocity;
}
if (sphere.GetComponent().movedLeft)
{
direction = Vector3.left;
Vector3 velocity = speed * direction;
transform.position -= velocity;
}
if (sphere.GetComponent().movedRight)
{
direction = Vector3.left;
Vector3 velocity = speed * direction;
transform.position += velocity;
}
}
}