Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 Ugniues · Apr 02, 2015 at 07:31 PM · c#unity 52dnullreferenceexception

Referencing variables from another script

Hello,

I have a short question. I'm trying to access variable from another script called "weapon": alt text

Everything works good, but when I change weapon (gameObjects with same Tag). I get error: alt text

Is there any way to solve this? I have no idea how.

Thank you.

Comment
Add comment · Show 2
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 Graham-Dunnett ♦♦ · Apr 02, 2015 at 07:35 PM 0
Share

I re-tagged your question to add it to the list of over 1 million questions about NullReferenceExceptions. (I might have over exaggerated the 1 million.)

avatar image DoTA_KAMIKADzE · Apr 02, 2015 at 07:41 PM 0
Share

Relying on tag is a bad idea, but if you do so then most likely you either put that code not in the right place or you don't call it once again when you switch weapon, or you don't remove the previous one properly, or...can't really guess which one from that part of code.

$$anonymous$$ost likely you just utilize reference to previous "deleted" object, hence the error cose it's null. $$anonymous$$ake sure that there is only one GameObject with tag "Weapon" or alternatively use FindGameObject*s*WithTag and walk through them to find the one you need which != null. Still this is a bad idea to rely on tag.

If you'll not be able to detect more precisely what goes where and why you get null error then be so kind to provide bigger part of your code so that someone can locate what you're missing.

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by Graham-Dunnett · Apr 02, 2015 at 07:34 PM

Your code looks up game objects tagged with Weapon. Maybe none are found, in which case your next line is going to go wrong. I suspect that is what is causing the NullReferenceException. Or, maybe the weapons variable does have a GameObject, but that GameObject doesn't have a weapon component on it. In which case the Debug.Log line will cause a NullReferenceException. As I am sure you'll know from reading all the questions on this site tagged with NullReferenceException this happens whenever a variable isn't set to a value. You should never assume that a variable is assigned, so check that before using the variable.

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

Answer by Ugniues · Apr 02, 2015 at 09:24 PM

Found my problem! Thank you! I left parent taged with "weapons". I removed tag and left only on childs, so it works.

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

How do you handle progressive event calls? 0 Answers

How to move rigidbody a certain distance, rather than constantly 1 Answer

Sprite image not changing 0 Answers

The type or namespace name `Events' does not exist in the namespace `TouchScript' 1 Answer

How to make 3 bullets fire at different angles 1 Answer


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