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 OgiJr · May 23, 2016 at 05:28 PM · gameobjectintcompare

How do I compare the same int in two different GameObjects?

Hi, is it possible to compare the same variable (there is a script with int a) in two different gameObjects public GameObject go1; public GameObject go2; they both have the script and I did assign them in the #scene but whenever I try to state if(GO1.a == GO2.a)... it tells me that neither one of them are defined. Is it possible for me to state it in another so that it works?

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
0
Best Answer

Answer by corn · May 23, 2016 at 08:14 PM

It seems you misunderstand how GameObjects work.

A GameObject is just an entity in a scene. By itself, it is pretty much useless, but its purpose is to hold Components, which are various objects you attach to a GameObject. Transform is a Component, it represents the GameObject's position in the scene space, a Sprite is a Component, a Renderer, a RigidBody... anything you attach to a GameObject is a Component. That includes the scripts you wrote.

So let's say you have a script with a public int attribute :

 public class MyScript : 
 {
     public int myInt = 0;
 }

What happens when you attach it to a GameObject (myGameObject) ? It means that myGameObject will have a Component that is an instance of MyScript. But myGameObject won't magically get a myInt attribute, you cannot access this value directly from the GameObject. You need to get a reference to the MyScript Component that is attached to myGameObject, and then get the myInt attribute of said Component.

So here's your script with your two GameObjects :

 public class CompareInts : MonoBehaviour
 {
     public GameObject go1;
     public GameObject go2;
 }

After you assign them in the scene, you have references to your two GameObjects, but you still can't read the myInt values. So the next step is to get references to their MyScript Components. For this, you need to use GetComponent.

So in MyScript, write :

 private void Compare()
 {
     MyScript myScript1 = go1.GetComponent<MyScript>();
     MyScript myScript2 = go2.GetComponent<MyScript>();
 
     if (myScript1.myInt == myScript2.myInt)
     {
         // Whatever.
     }
 }

And then you have it, you got your two ints and you can compare them.

Comment
Add comment · Show 2 · 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 OgiJr · May 23, 2016 at 09:39 PM 0
Share

Thank you very much! You explained it in detail and I was able to understand it- you are the best!

avatar image corn OgiJr · May 23, 2016 at 09:58 PM 0
Share

You're welcome ! Don't forget to carefully read the Unity $$anonymous$$anual, you'll find very valuable information there. (:

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

comparing two gameobject 1 Answer

Why cant I get the object I want (UTS) 0 Answers

Get every possible combination of elements in a list 1 Answer

How do I get my weapon to stop firing if the ammo <= 0 1 Answer

Comparing GameObjects without using name or tag. 0 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