- Home /
C# : comparison of type between gameobject of specific type
Hello all,
First I thank you for all the other questions and answer you already have done, because it helped me a lot for most of my problems. It's really nice to have all this help stored here, and it's avoid me to ask questions which have already been answered.
I have a question, and possibly it has been replied somewhere, but I didn't find it. My problem is the following : I am trying to test the type of an object, and it's always returning true. The code is following :
using UnityEngine;
using System.Collections;
public class TileScript : MonoBehaviour {
private GameObject TileContent;
[...]
public void TileAffectObject(GameObject objectassigned)
{
this.TileContent=objectassigned;
objectassigned.transform.localPosition = new Vector3(this.transform.localPosition.x,0,this.transform.localPosition.z);
}
public bool TileIsFree()
{
if( this.TileContent == null)
return true;
else
return false;
}
public bool TileContentIs(GameObject comparison)
{
if(TileIsFree())
return false;
if(TileContent.GetType() == comparison.GetType())
return true;
return false;
}
}
I think this because the comparison is between two GamObject, and that's probably where I am wrong, but for example one is an empty object with a script A, and the other is a cube with a script B, and I want to be able to store both of them in the object TileScript.
Thanks for your time.
el_kloklo
Answer by paulaceccon · Dec 26, 2012 at 11:43 PM
Yeah, what is happening is what you've said before. Both are game objects, so the comparison always return true. I didn't understand how do you would like this to behave. Did you think about using tags and maybe compare the objects' tags?
Answer by Bunny83 · Dec 26, 2012 at 11:53 PM
A GameObject is just a container. There are no other types that are derived from GameObject. The GameObject class can't even be derived. Your classes are derived from MonoBehaviour which is a Component. Components are attached to GameObjects. We don't know how your game setup looks like, so it's hard to tell what this code should actually do.
Answer by el_kloklo · Dec 27, 2012 at 10:18 PM
Hello, thanks for your answers and merry christmas.
I am searching a way for comparing two objects which are contained as a gameobject. Maybe the tag could be a solution, but I don't know how it's work.
Another solution I am currently looking in is making the TileContent stored as myclass which return a static string identifiant, and all the object I want to store in derivate from the class myclass, and overload the identifiant, but I have problem with overloading static method.
I don't know if I am clear, but maybe a schematics could help a bit :
Thanks for your time
Your answer
Follow this Question
Related Questions
Distribute terrain in zones 3 Answers
Multiple Cars not working 1 Answer
gameObject are not referenced 2 Answers
Ordering a list of GameObjects 3 Answers