Quantcast
Channel: Answers for "How to compare if two gameObjects are the same?"
Viewing all articles
Browse latest Browse all 4

Answer by Mouton

$
0
0
**[Edit:]** Changed the code to match your needs + some further informations below What the OP needs is not comparing if two GameObject references point to the same GameObject but compare if the "Visual Informations" are the same. In order to do so, you can add a component to all the clickable GameObjects with a logic to check if the visual information matches to the reference one In my example, I will use a Picture, but you can do the same with a sound, a phrase, anything that should be considered similar by humans. public class ReferencePicture: Monobehaviour { public ClickablePicture matchingPicture; void Start() { MatchANewPicture(); } public void MatchANewPicture() { if (matchingPicture != null) { matchingPicture.referencePicture = null; matchingPicture = null; } // there may be new pictures since we last checked ClickablePicture[] clickablePictures = GameObject.FindObjectsOfType() ClickablePicture matchingPicture = clickablePictures[Random.Range(0, clickablePictures.Length)]; matchingPicture.referencePicture = this; } // We don't want the pictures to get different, // so we change both reference & matching void OnChangePicture(Material material) { this.GetComponent().material = material; matchingPicture.GetComponent().material = material; // if you want to do some further job inside 'ClickablePicture' class // matchingPicture.OnChangePicture(material); } } With such a ReferencePicture class, the ClickablePicture class gets trivial: public class ClickablePicture : Monobehaviour { public ReferencePicture referencePicture; public void OnClick() { if (reference == null) Debug.Log("Wrong !"); else { Debug.Log("Correct !"); referencePicture.MatchANewPicture(); } } } Now you have a safe Reference / Clickable connection set very easily. ---------- > You are right, they can't have more then one GameObject, but two different GameObjects' Transforms can be equal. Not correct. Transforms are of **reference type**. When you compare two **references**, you compare their **adresses in memory**, not their **value**. This is why we say they are **reference type** and not **value type**. It wouldn't be efficient to compare if the position, rotation & scale of your transforms are the same. GameObject firstObject = new GameObject (); GameObject clickedFirstObject = firstObject; Debug.Log (firstObject == clickedFirstObject); // true Debug.Log (firstObject.transform == clickedFirstObject.transform); // true GameObject clickedOtherObject = new GameObject (); firstObject.transform.position = new Vector3 (1, 2, 3); firstObject.transform.rotation = new Quaternion (0.1f, 0.2f, 0.3f, 0f); firstObject.transform.localScale = new Vector3 (1, 2, 3); clickedOtherObject.transform.position = firstObject.transform.position; clickedOtherObject.transform.rotation = firstObject.transform.rotation; clickedOtherObject.transform.localScale = firstObject.transform.localScale; Debug.Log (firstObject.transform == clickedOtherObject.transform); // false Debug.Log (firstObject.transform == clickedFirstObject.transform); // still true To be sure, we double check with the method Object.ReferenceEquals(a, b) and the result are the same (this is pretty much what the "==" operator does for a **reference type**) Debug.Log (Object.ReferenceEquals (firstObject, clickedOtherObject)); // false Debug.Log (Object.ReferenceEquals (firstObject, clickedFirstObject)); // true And this is true with every **reference type**. The "==" operator is overloaded so **reference types** compare their reference (thing they point in memory). Now, let's consider this: int a = 1; int b = 1; Debug.Log (a == b); // not the same reference, but true Debug.Log (Object.ReferenceEquals (a, b)); // of course, this is false **int** is a **value type,** so the "==" operator is overloaded so **value types** compare their value. Why does **reference types** works that way ? Because an addresse in memory is a relatively small data to store, and you don't want to copy a 100MB class whenever you want to use it, so when you do "objA = objB", you just create a new reference. However, using GameObject.Instantiate(objA) will create a new instance using it's own memory and getting it's own reference. I hope it will help to make the distinction between an object, it's value and it's reference. You should also consider to visit these links. - [Unity - Data Types][1] - [C# value types table][2] [1]: https://unity3d.com/learn/tutorials/modules/beginner/scripting/data-types [2]: https://msdn.microsoft.com/en-us/library/bfft1t3c.aspx

Viewing all articles
Browse latest Browse all 4

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>