Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
1
Question by NovaDrox · Dec 13, 2016 at 04:05 AM · variablevariablescompare

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();
 }


Comment
Add comment · Show 12
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image getyour411 · Dec 13, 2016 at 04:26 AM 0
Share

I assume your error is from Gun Script line 4? Are these two scripts on the exact same GameObject (not on different child objects)?

avatar image NovaDrox · Dec 13, 2016 at 06:04 AM 0
Share

No, one is on my player and the other is on my actions.

avatar image superbsumit · Dec 13, 2016 at 02:28 PM 0
Share

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>();
 }
avatar image NovaDrox superbsumit · Dec 13, 2016 at 03:44 PM 0
Share

I tried that, but it's saying error at the If (ammo_clip.Clip_Size > 0)

avatar image superbsumit superbsumit · Dec 13, 2016 at 07:15 PM 0
Share

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.

avatar image NovaDrox superbsumit · Dec 13, 2016 at 07:29 PM 0
Share

No, my ammo_clip script is in my actions area and the gun script is on my player.

Show more comments

2 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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();
      }
  }
Comment
Add comment · Show 7 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image NovaDrox · Dec 16, 2016 at 03:26 AM 0
Share

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?

avatar image TBruce NovaDrox · Dec 16, 2016 at 04:08 AM 0
Share

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!

avatar image NovaDrox TBruce · Dec 16, 2016 at 04:21 AM 0
Share

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.

Show more comments
avatar image NovaDrox · Dec 16, 2016 at 05:05 AM 1
Share

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();
         }
     }
 }

avatar image TBruce NovaDrox · Dec 17, 2016 at 12:56 AM 0
Share

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!

avatar image NovaDrox TBruce · Dec 17, 2016 at 03:02 AM 0
Share

Okay and sorry.

avatar image
1

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>();
Comment
Add comment · Show 6 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image NovaDrox · Dec 13, 2016 at 06:29 AM 0
Share

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.

avatar image NovaDrox · Dec 13, 2016 at 01:02 PM 0
Share

Nope, that did not work. I get the error again.

avatar image NovaDrox · Dec 13, 2016 at 01:37 PM 0
Share

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.

avatar image getyour411 NovaDrox · Dec 14, 2016 at 06:20 AM 0
Share

Post your updated code; the error on 9 (assu$$anonymous$$g same error) is basically because 4/ammo_clip reference creation is failing

avatar image NovaDrox getyour411 · Dec 14, 2016 at 01:09 PM 0
Share

What do you mean by that?

Show more comments

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

6 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image

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

Setting a variable on instantiation, but when the object is enabled the variable is set back to default. 0 Answers

Sorting by Variables Help 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges