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 Xysch · Nov 28, 2014 at 05:02 AM · c#variableintaccess

Accessing other script variables error

I am trying to have a script check if a variable is not equal to 0 in another script. This the error I'm getting:

NullReferenceException: Object reference not set to an instance of an object EnemyAI.Update () (at Assets/Scripts/EnemyAI.cs:20)

Code accessing other code:

 void OnTriggerEnter2D(Collider2D col){
             if (col.gameObject.tag == "Bullet") {
                 var move = gameObject.GetComponent<MoveTrail>();
                 if(move.moveSpeed != 0){
                     air = air - 10;
                 }
             }
     }


Script MoveTrail's moveSpeed is an int.

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 bhavinpanara22 · Nov 28, 2014 at 05:05 AM 0
Share

i am not sure. but i think you are trying to access variable of a c-sharp script from your javascript file. I mean, scripting language of both your scripts are different.

avatar image Eric5h5 · Nov 28, 2014 at 05:10 AM 0
Share

There's no indication that would be the case.

1 Reply

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

Answer by Eric5h5 · Nov 28, 2014 at 05:12 AM

You neglected to say what line the error occurred on, but I assume it's "if(move.moveSpeed != 0){". That means the "move" variable is null, since evidently there is no MoveTrail component attached to gameObject. I'd recommend getting in the habit if checking whether variables are null before attempting to use them.

Comment
Add comment · Show 3 · 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 Xysch · Nov 28, 2014 at 05:19 AM 0
Share

Does the $$anonymous$$oveTrail script have to be part of the same GameObject, or are they able to be part of two different gameObjects? And the error occurred while the game was running.

avatar image bhavinpanara22 · Nov 28, 2014 at 05:23 AM 1
Share

since you are writing "gameObject.GetComponent();", $$anonymous$$oveTrail must be on your gameObject. If you want to access the $$anonymous$$oveTrail from another game object in scene then try, "GameObject.FindGameObjectWithTag("Tag Of other game object").GetComponenent()"

avatar image Eric5h5 · Nov 28, 2014 at 05:25 AM 0
Share

Yes, the error occurred while the game was running, but you didn't say which line of code. ;) The error says line 20, but you don't have 20 lines in your question. The $$anonymous$$oveTrail script has to be attached to gameObject, since you're doing gameObject.GetComponent. $$anonymous$$aybe you meant col.gameObject.GetComponent.

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

27 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

Related Questions

Accessing variable from a method in another script and gameObject 2 Answers

Can I access variables of scripts that inherit from abstract classes? 2 Answers

How to access variable from other class? 1 Answer

string + int = variable 2 Answers

how to acess Static variable in other scripts without extended functions? 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