using UnityEngine;
using System.Collections;
public class PlayerController : MonoBehaviour {
float speed = 10f;
bool facingRight = true;
Animator anim;
public GameObject playerBullet;
void Start ()
{
anim = GetComponent<Animator> ();
}
void Update ()
{
//Player Movement system
float moveHor = Input.GetAxisRaw ("Horizontal");
transform.Translate (moveHor * speed * Time.deltaTime, 0, 0);
anim.SetFloat ("SpeedHor", Mathf.Abs (moveHor));
float moveVert = Input.GetAxisRaw ("Vertical");
transform.Translate (0, moveVert * speed * Time.deltaTime, 0);
anim.SetFloat ("SpeedVert", moveVert);
if (moveHor > 0 && !facingRight)
FlipHor ();
else if (moveHor < 0 && facingRight)
FlipHor ();
//Player attacking system - Instantiate a prefab bullet and project it
//in the direction the player is facing x number of units forward.
if (Input.GetKeyDown ("space"))
Instantiate (playerBullet, transform.position, transform.rotation);
}
void FlipHor()
{
facingRight = !facingRight;
Vector3 theScale = transform.localScale;
theScale.x *= -1;
transform.localScale = theScale;
}
}
I’m getting the error
UnassignedReferenceException: The variable playerBullet of PlayerController has not been assigned.
You probably need to assign the playerBullet variable of the PlayerController script in the inspector.
UnityEngine.Object.Internal_InstantiateSingle (UnityEngine.Object data, Vector3 pos, Quaternion rot) (at C:/BuildAgent/work/d63dfc6385190b60/artifacts/EditorGenerated/UnityEngineObject.cs:74)
UnityEngine.Object.Instantiate (UnityEngine.Object original, Vector3 position, Quaternion rotation) (at C:/BuildAgent/work/d63dfc6385190b60/artifacts/EditorGenerated/UnityEngineObject.cs:84)
But I have assigned the variable in the inspector. This leads me to believe that my code is incorrect, but I don’t see how that is the case. I’m totally lost on this one. Just trying to instantiate a bullet prefab but this is holding me up. Anyone that can explain the issue here?