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 LuckierBread · Jul 16, 2016 at 04:35 AM · variableaccessing scriptschecking

checking other object variable

Hi people,

I would like to ask if it is possible to check a variable residing in another object.

I have already searched through the unity script reference, but it doesn't answer the question http://docs.unity3d.com/410/Documentation/ScriptReference/index.Accessing_Other_Game_Objects.html

I don't want to call a function and i don't want to add,subtract, or translate, addforce etc.

I would like to check if a bool on object A is true or false so that object B may react accordingly

object B will be spawned many times and at many different intervals while Object A is always there until it collides with object B and Object A's bool is true.

The trick is that the player will be changing the bool at will.

So to summarise the question.

How do i get Object B to check if Object A's bool is true or false on contact?

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

Answer by CanCo · Jul 16, 2016 at 05:47 AM

Say your first script is just

 public class code1 : MonoBehaviour {
     public bool foo = true;
 }

then you'll need another code which is like:

 public class code2 : MonoBehaviour {
     public code1 _code;
     void Start () {
         if(_code.foo == true)
             doSomething();
     }
 }

(This is written in C#)

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 LuckierBread · Jul 16, 2016 at 06:14 AM 0
Share

it always returns this error

NullReferenceException: Object reference not set to an instance of an object ContactDeath.OnTriggerEnter (UnityEngine.Collider other) (at Assets/Scripts/ContactDeath.cs:32)

The line of code that it is referring to is this

 if(colorChanger.isRed == true)
avatar image LuckierBread · Jul 16, 2016 at 06:27 AM 0
Share

Just figured out a solution.

not 100% on why what you said didn't work

but because it was an on trigger enter script i just used

 if(other.GetComponent<ColorChanger>().isRed == true)

I'm going to give you an upvote though because that is how it is supposed to work.

avatar image CanCo · Jul 16, 2016 at 10:39 AM 0
Share

Remember that if you get a component of another object during runtime the program will be slower, so define a variable to hold the value of the ColorChanger class.

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

62 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

Related Questions

Change of static variable 1 Answer

Access variable of same script with many instances 1 Answer

How to send a variable from one script to another when the script finishes. 0 Answers

How to make a global variable in Unity? 5 Answers

[Help] Getting variable based on string value. 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