hi i am quite new to unity, i am trying to make a 2d game. Anyway, i trying to make my ship to shoot the bullet out. The code work but when i try to follow the tutorial about Inter-object communication ( basically separate the code out) i keep getting the error:
NullReferenceException: Object reference not set to an instance of an object
scrip.Update () (at Assets/scrip/Player_controller.cs:38)
well it point to: game_state.instance.Spawn_bullet(ship_gun.position);
this is my player_controller scrip
using UnityEngine;
using System.Collections;
public class scrip : MonoBehaviour {
public float speed = 1f;
Transform ship_gun;
public GameObject player_bullet;
// Use this for initialization
void Start () {
ship_gun = transform.Find("gun");
}
// Update is called once per frame
void Update () {
float deltaX = 0f, deltaY = 0f;
if (Input.GetKey(KeyCode.W))
{
deltaY -= 1f;
}
if (Input.GetKey(KeyCode.S))
{
deltaY += 1f;
}
if (Input.GetKey(KeyCode.D))
{
deltaX += 1f;
}
if (Input.GetKey(KeyCode.A))
{
deltaX -= 1f;
}
// fire bullet
if (Input.GetKey(KeyCode.Space))
{
game_state.instance.Spawn_bullet(ship_gun.position);
}
//update moverment
transform.Translate(new Vector3(deltaY, deltaX, 0f) * Time.deltaTime * speed);
}
}
and this is my game_state scrip
using UnityEngine;
using System.Collections;
public class game_state : MonoBehaviour
{
[SerializeField]
GameObject player_bullet;
public GameObject Spawn_bullet(Vector3 _position)
{
return (GameObject)Instantiate(player_bullet, _position, Quaternion.identity);
}
public static game_state instance = null;
void alwake ()
{
if (instance != null)
{
Debug.LogError("all found");
}
instance = this;
}
}
Well i am quite unsure what is wrong with it, the tutorial is unclear about where to add the game_state scrip to so i am quite unclear about the scrip is wrong or i am doing something else wrong
Thank for helping me