The question is answered, right answer was accepted
Object reference not set to an instance of an object
I am getting "Object reference not set to an instance of an object" error when I'm calling "bullet.Shooting(bulletStartPos)" function. "ReadyShootingPosition()" is called by animation event. This error appears only when I'm using animation. "Shooting()" function seems to be fine in "Bullet" class and is public. What did I do wrong ?
using UnityEngine;
using System.Collections;
public class PlayerControllerShoot : MonoBehaviour {
private Bullet bullet;
private Vector3 bulletStartPos;
private Animator anim;
void Start () {
bullet = GameObject.FindObjectOfType<Bullet> ();
anim = GetComponent<Animator> ();
}
void Update () {
if (Input.GetKey (KeyCode.Space)) {
anim.SetBool ("isShooting", true);
} else if (Input.GetKeyUp (KeyCode.Space)) {
anim.SetBool ("isShooting", false);
}
}
void ReadyShootingPosition() {
bulletStartPos = transform.GetChild (0).localPosition;
bullet.Shooting (bulletStartPos); //error here
}
}
$$anonymous$$ake bullet public and check in inspector if FindObjectOfType() actually finds this object on Start().
Check also ReadyShootingPosition() is not called by animation before PlayerControllerShoot .Start().
Thank you, it seems that FindObjectOfType() couldn't find it, and that's because my Bullet wasn't in the scene. Didn't know that it had to be in it, but now I'll be aware of that. Thank you again !
Follow this Question
Related Questions
NullReferenceException: Object reference not set to an instance of an object 3 Answers
Create group people, Select one or more at random. 1 Answer
Why do I keep getting this error? 1 Answer
[Begginer] [Solved] How to solve a Null Reference Exception 1 Answer
CameraController (object reference not set to an instance of an object) 1 Answer