Object reference not set to an instance of an object (C#)
I know, I know... This has been asked all over the place, but I personal don't get it. Could someone help me out. I am trying to use my Ammo_Size float from my first script and use it in my gun script so I can compare the Ammo_Size number in the gun script.
Gun Script:
void Start()
{
Ammo_Clip ammo_clip = GetComponent<Ammo_Clip>();
}
public void Update()
{
if (ammo_clip.Clip_Size > 0)
{
Gun_Controll();
}
}
Ammo_Clip Script:
public float Ammo_Size;
public float Clip_Size;
public Text Ammo_Clip_Display;
private void Start ()
{
Ammo_Size = 36;
Clip_Size = 12;
Display_Ammo_Clip();
}
I assume your error is from Gun Script line 4? Are these two scripts on the exact same GameObject (not on different child objects)?
Your 'ammo_clip' object is initialized as a private object in the Start method. $$anonymous$$ake it global and it will work.
Ammo_Clip ammo_clip;
void Start() {
ammo_clip = GetComponent<Ammo_Clip>();
}
I tried that, but it's saying error at the If (ammo_clip.Clip_Size > 0)
Okay! do you have both the scripts attached to the same object? According to the error your 'ammo_clip' object is null. So test it by adding some Debug.Log's at Start method.
No, my ammo_clip script is in my actions area and the gun script is on my player.
Answer by TBruce · Dec 16, 2016 at 02:53 AM
Hi @Nova_Tech,
This is your error
if (ammo_clip.Clip_Size > 0)
The reason you are getting an error is that ammo_clip
is null. The reason it is null is because you have not attached a Ammo_Clip
to the game object that the Gun Script
component is attached to.
To fix this first make sure that ammo_clip
is posted as a global like so
private Ammo_Clip ammo_clip;
Now in the Start()
function set the ammo_clip
reference like so
void Start()
{
ammo_clip = GetComponent<Ammo_Clip>();
}
Now when you go to you the ammo_clip
do the following to ensure that you do not get an error
public void Update()
{
// make sure that ammo_clip is valid before trying to use it
if ((ammo_clip != null) && ammo_clip.Clip_Size > 0))
{
Gun_Controll();
}
}
Thank you so much!!! I have been looking for answer to this error for a bit now.
Oh, I know this is not on the topic of this post, but could you and your knowledge help me out on something else?
It all depends on what you mean by off topic
. The FAQ does state
Don't post multiple unrelated questions in one post
and to
Break multi-part problems into simpler questions
which is what I suggest. But you can ask and I will let you know.
In the meantime could you please be so kind as to click the "Accept" button above to accept the answer since you found this answer helpful? Thank you!
Well, its still code related. I have my gun that my ClipSize is attached to. $$anonymous$$y gun only shoots in 1 direction, and I want it to shoot where my mouse is, but I don't know how.
It does fit. Here it is.
using UnityEngine;
using System.Collections;
public class Shoot : $$anonymous$$onoBehaviour
{
public GameObject Bullet;
public GameObject Bullet_Spawner;
public float Change_Despawn_Bullet;
public float Bullet_Forward_Force;
public float WaitTimeRelaod;
private Damage_Heal ammo_clip;
private void Start()
{
ammo_clip = GetComponent<Damage_Heal>();
ammo_clip.UpdateAmmo_ClipBar();
ammo_clip.AmmoSize = 36;
ammo_clip.ClipSize = 12;
}
public void Update()
{
if ((ammo_clip != null) && ammo_clip.ClipSize > 0)
{
Gun_Controll();
}
if (Input.Get$$anonymous$$eyDown("r"))
{
StartCoroutine("Reload_Animation");
}
}
IEnumerator Reload_Animation()
{
while (ammo_clip.ClipSize >= 0 && ammo_clip.ClipSize < 12 && ammo_clip.AmmoSize > 0)
{
yield return new WaitForSeconds(0.5f);
ammo_clip.AmmoSize -= 1;
ammo_clip.ClipSize += 1;
ammo_clip.UpdateAmmo_ClipBar();
}
}
public void Gun_Controll()
{
if (Input.Get$$anonymous$$ouseButtonDown(0))
{
GameObject Temporary_Bullet_Handler;
Temporary_Bullet_Handler = Instantiate(Bullet, Bullet_Spawner.transform.position, Bullet_Spawner.transform.rotation) as GameObject;
Temporary_Bullet_Handler.transform.Rotate(Vector3.left * 90);
Rigidbody Temporary_RigidBody;
Temporary_RigidBody = Temporary_Bullet_Handler.GetComponent<Rigidbody>();
Temporary_RigidBody.AddForce(transform.forward * Bullet_Forward_Force * 1000);
Destroy(Temporary_Bullet_Handler, Change_Despawn_Bullet);
ammo_clip.ClipSize -= 1;
ammo_clip.UpdateAmmo_ClipBar();
}
}
}
Hi @Nova_Tech,
Could you please post this as a separate question? You can address it to me by using @$$anonymous$$avina in the post. I will also include a demo package because a picture as they say is like a thousand words.
Thank you!
Answer by getyour411 · Dec 13, 2016 at 06:06 AM
Please don't post a comment/follow-up as an Answer - use the "Add comments" button or the "reply" link instead; I fixed this for you.
You need to fix 4 to be something like
Ammo_Clip ammo_clip = GameObject.Find("myThing").GetComponent<Ammo_Clip>();
Ok, and sorry about that last post. Hopefully I get it right this time.
Going back to the subject at hand, I can try that. I'll let you know if it works later.
Sorry for the side comment, but I just remembered that when I click the error on the console, it takes me to line 9 with the if statement.
Post your updated code; the error on 9 (assu$$anonymous$$g same error) is basically because 4/ammo_clip reference creation is failing
Your answer
![](https://koobas.hobune.stream/wayback/20220612102617im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Comparing a variable (The right answer) to user input. 2 Answers
Accessing a text variable from another script 0 Answers
Using variables values from Yarn Spinner 0 Answers
Sorting by Variables Help 0 Answers