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 Wrymn · Mar 20, 2013 at 08:05 PM · variableifcompare

Comparing variable from other script

Hello, after few days I gave up and came to you guys :D What I am trying to do is:

In Script 1 I want to compare variable:

 if(Variable WOOD from Script2 >= 50)
      DoSomething


In Script2 which is attached to another object I have variable WOOD.

 var WOOD = 100;


I need to compare Variable wood I tried to ask my friend, I searched whole google, Found few posts tried every one of them Still the same Error: NullReferenceException: Object reference not set to an instance of an object

Pleasee help

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

1 Reply

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

Answer by whebert · Mar 20, 2013 at 08:23 PM

 // You can find a reference to Script2 like this, assuming it is attached to some game object in your scene.
 var script2 : Script2 = FindObjectOfType(Script2);
 
 // You should always check to make sure script2 was found before trying to use it
 if(script2)
 {
    if(script2.WOOD >= 50)
    {
       // Do Something
    }
 }
 else
 {
    // You didn't find a Script2 in your scene
 }
Comment
Add comment · Show 9 · 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 Wrymn · Mar 20, 2013 at 08:47 PM 0
Share

It did not find that object in a scene. I tried:

 if(script2)
         Debug.Log("Found");
 else
         Debug.Log("did not found");



and ofc it printed did not found.

avatar image whebert · Mar 20, 2013 at 09:38 PM 0
Share

Be sure to replace "Script2" in my example with whatever the name of your script is.

avatar image Wrymn · Mar 20, 2013 at 09:59 PM 0
Share

Of Course I did I am not that stupid :) This is really paranormal activity jesus, how that it is not working? Impossible.

avatar image whebert · Mar 20, 2013 at 10:07 PM 0
Share

Sorry, was not implying you were. :)

And you're sure there is a game object with your second script attached in the scene? That should work then... You could put Debug.Log statements in the Start function of you scripts just to verify they are in the scene and active, "Script 1 started", "Script 2 started", etc.

avatar image whebert · Mar 20, 2013 at 10:40 PM 1
Share

I believe you need to name your script something other than "Resources". That is an already existing utility class defined for Unity.

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

12 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

Related Questions

Multiple Cars not working 1 Answer

Random number on game start for a variable 1 Answer

Unexpected token: if 1 Answer

ANOTHER Boolean Problem 1 Answer

How does Unity reads the "If" conditions? 3 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