nes420
1
Hey guys,thx for trying to help me out this one looks alot like one of another thread on this forum but it doesn’t work for me.
Im having this Ammo script here
using UnityEngine;
using System.Collections;
public class Ammo : MonoBehaviour {
public float ammoSticky =5f;
public float ammoBullet =999f;
public float ammoPlasma = 10f;
}
this is my shooting class
using UnityEngine;
using System.Collections;
public class Shooting : MonoBehaviour {
public GameObject bullet_prefab;
public GameObject sticky_prefab;
public GameObject plasma_prefab;
public float bulletImpulse = 100f;
public float stickyImpulse = 100f;
public float cooldown = 0.1f;
float coolDownRemaining = 0;
public Ammo ammo;
// Use this for initialization
void Start () {
ammo = GetComponentInChildren<Ammo>();
}
// Update is called once per frame
void Update () {
coolDownRemaining -= Time.deltaTime;
if (Input.GetButtonDown ("Fire1")) {
Camera cam = Camera.main;
GameObject theBullet = (GameObject)Instantiate(bullet_prefab, cam.transform.position + cam.transform.forward, cam.transform.rotation) ;
theBullet.rigidbody.AddForce(cam.transform.forward * bulletImpulse,ForceMode.Impulse);
}
//&&
if (Input.GetButtonDown ("Fire2") && coolDownRemaining <= 0 && ammo.ammoSticky >= 0) {
coolDownRemaining = cooldown;
Camera cam2 = Camera.main;
GameObject theSticky = (GameObject)Instantiate(sticky_prefab, cam2.transform.position + cam2.transform.forward, cam2.transform.rotation) ;
theSticky.rigidbody.AddForce(cam2.transform.forward * stickyImpulse,ForceMode.Impulse);
ammo.ammoSticky -= 1;
}
if (Input.GetButtonDown ("Fire3")) {
Camera cam2 = Camera.main;
GameObject theSticky = (GameObject)Instantiate(plasma_prefab, cam2.transform.position + cam2.transform.forward, cam2.transform.rotation) ;
theSticky.rigidbody.AddForce(cam2.transform.forward * stickyImpulse,ForceMode.Impulse);
}
}
}
I keep getting this error:
NullReferenceException: Object reference not set to an instance of an object
Shooting.Update () (at Assets/Scripts/Shooting.cs:32)
, altough I made the same steps as in here (the solved version at the end) link text
thank you
If that line n umber is correct, then ammo is null.
Whenever you get a null reference error, see what is being referenced in that line.
Then put a Debug.Log just before that line that prints it to confirm it is null (there may be more then one
'reference, this is the way to find out which is the problem.)
Looking at your code I would say it is likely that this script is on a GameObject that does not have an Ammo component.
Its possible you accidentally put a copy of this script on the wrong object.
You can find all objects that have this component by typing its name, Shooting, in the hierarchy search bar.
nes420
3
Exactly, what I finally figured out, I have created a new empty component withing the game
assigned it the ammo script then During the game i call that empty objects Script component. and it works.
That way the ammo quantity is persisted and i can access it from different scripts.
Dont know if it’s a good implementation but, maybe I could implement a Singleton dunno know how that works in unity.
Thank you either way.