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 /
avatar image
0
Question by bmalloy0 · Aug 02, 2019 at 04:59 PM · variableerror messagegameplay

Why am I getting this error message? (C#)

I am using the following code in a game I am developing (irrelevant code removed):

 public class WeaponSpawner : MonoBehaviour
 {
     GameObject weapon;
     
     public void UseWeapon()
     {
         Instantiate(weapon, transform.position, Quaternion.Euler(direction), transform);
     }
 
     public void UpdateWeapon(GameObject newWeapon)
     {
         weapon = newWeapon;
     }
 }

I call the UpdateWeapon() method from the game menu when changing the active weapon, and I call the UseWeapon() method when actually attacking.

For some reason, however, even though the UpdateWeapon() works fine (confirmed with Debug.Log() and inspector while game is running) assigning the new weapon, when I attempt to call UseWeapon() afterwards, I get the following error:

"UnassignedReferenceException: The variable weapon of WeaponSpawner has not been assigned."

Edit: Added in some Debug.Log and tried a different way of instantiating:

     public void UseWeapon()
     {
         Debug.Log(weapon);
         GameObject createdWeapon = Instantiate(weapon,transform.parent);
     }
 
     public void UpdateWeapon(GameObject newWeapon)
     {
         Debug.Log("Current physical weapon: " + weapon);
         Debug.Log("Switching physical weapon to: " + newWeapon);
         weapon = newWeapon;
         Debug.Log("New physical weapon: " + weapon);
     }

Here is a readout of the Log:
alt text Why is this happening/what is going on?

error-message.png (25.4 kB)
Comment
Add comment · Show 4
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 AaronXRDev · Aug 02, 2019 at 05:22 PM 0
Share

If you log weapon on the line before the instantiate does it come back null?

avatar image bmalloy0 AaronXRDev · Aug 02, 2019 at 05:33 PM 0
Share

Yes. When logged before the assignment in UpdateWeapon(), it comes back null, when logged after it comes back properly, then when logged before Instantiate it's null again.

avatar image AaronXRDev bmalloy0 · Aug 02, 2019 at 05:37 PM 0
Share

Is NewWeapon being passed as a prefab?

Show more comments

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Vega4Life · Aug 02, 2019 at 05:39 PM

It has to be because you are calling UseWeapon before you are calling UpdateWeapon. If you do that, weapon is null. So make sure you either are doing a null check on weapon (in the UseWeapon() method), or just make sure that situation can't happen.

Comment
Add comment · 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

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

118 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 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Could not preload global game manager #0 0 Answers

How to change a variable in the same script? 3 Answers

Unity says my GameObject variable is not set, but it is. 2 Answers

variable has not been assigned? 1 Answer

Array in JS NOT Working with Components?! 2 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