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
0
Question by D3mon1zA · Sep 06, 2016 at 08:48 AM · weaponobject-reference-errorequip

Object Reference not set to an instance of an object

Hey guys!

So I am working on a character controller script and I have run into an issue I can't figure out. I have my PlayerManager script set up with formulas to calculate hit points depending on whether you have a weapon equipped or not and if you do then it adds then weapons damage to your total hitpoints.

now where I am having issue is in this function here:

 void equipWeapon()
  {
      if (Input.GetKeyDown(KeyCode.E))
      {
          GameObject.Find("Weapon").GetComponent<Weapon>().equipped = true;
          GameObject.Find("PlayerWeapon").GetComponent<Weapon>().thisWeapon();
  
          wepDam = GameObject.Find("PlayerWeapon").GetComponent<Weapon>().weaponDam;
 
      }
      else
      {
          GameObject.Find("Weapon").GetComponent<Weapon>().thisWeapon();
          wepDam = GameObject.Find("Weapon").GetComponent<Weapon>().weaponDam;
      }
 
 }

This is called from the _PlayerManager().Update(); function, and it is meant to go like this: if Key E is pressed then set boolean equipped(In the weapon script) to true, then start the function Weapon().thisWeapon(); function is called which looks like this :


               

 public void thisWeapon()
 {
 
     if (equipped == true)
     {
         weaponDam = Random.Range(4, 15);
         this.name = "PlayerWeapon";
         Debug.Log(this.name + " Equipped!");
     }
     else
     {
         weaponDam = 0;
     }
 }

All is fine until E is pressed, then I get the Error Object Reference not set to an instance of an object. But the script seems to still run if equipped == true ,the object name changes and the value of the hit points change as well...

any insight on what may be causing this error would be much appreciated as I have kinda hit a brick wall with it haha TIA

Comment
Add comment · Show 3
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 tanoshimi · Sep 06, 2016 at 08:54 AM 0
Share

What is not set to an instance of an object? Error messages are there to help you - please include them in full when asking for help.

avatar image D3mon1zA tanoshimi · Sep 06, 2016 at 09:23 AM 0
Share

Sorry I wasn't sure what was causing the error, I have resolved, Just needed to put in an extra check in to stop the loop from Update();

avatar image D3mon1zA · Sep 06, 2016 at 09:17 AM 0
Share

Problem Solved!

I realised that since I was calling this function from update and then changing the object name withing the function that is where the error was co$$anonymous$$g from. so to remedy this I have made a bool named isEquipped and set it to false, so when the update calls the function it will only do anything if isEquipped is false if so then it takes appropriate action, if true then it will be more or less ignored.

Update looks like:

  void equipWeapon()
     {
         if (isEquipped == false)
         {
             if (Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.E))
             {
                
                     isEquipped = true;
                     GameObject.Find("Weapon").GetComponent<Weapon>().equipped = true;
                     GameObject.Find("Weapon").GetComponent<Weapon>().thisWeapon();
 
                     wepDam = GameObject.Find("PlayerWeapon").GetComponent<Weapon>().weaponDam;
                     isEquipped = true;
 
             }
             else
             {
                     GameObject.Find("Weapon").GetComponent<Weapon>().thisWeapon();
                     wepDam = GameObject.Find("Weapon").GetComponent<Weapon>().weaponDam;
             }
 
 
         }
     }

 








0 Replies

· Add your reply
  • Sort: 

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

54 People are following this question.

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

Related Questions

Learning to equip weapons etc to character but its not working! 1 Answer

Needing help with my game i'm creating. 0 Answers

3rd person weapon equiping 1 Answer

How to create a link between particle and script C# 1 Answer

How to set gamobjects active on network? 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