- Home /
if game object is located at...
Is it possible to do this?
I can't seem to find out...
if(gameobject.position == (0,0,0))
{
debug.log("test")
}
or something like that anyways?
Answer by robertbu · Feb 24, 2014 at 11:37 PM
It would be like this:
if(gameObject.transform.position == Vector3(0,0,0))
{
debug.log("test")
}
In C# you would have to add 'new' in front of the Vector3(). Note that in programming it is usually not a good thing to directly compare floating point numbers (and a the x, y, and z components of a Vector3 are floating point numbers). A direct comparison of Vector3s works in Unity because under the hood Unity is checking to see if the two values are within a small threshold distance from each other. This makes a lot of code work well, but you will not necessarily have exactly the same values. FYI: Vector3.zero is shorthand for Vector3(0.0, 0.0, 0.0).
the issue is that "position" isn't a member of unitygameobject so it doesn't work. I was wondering if there is something like that I could use to check if a gameobject was at a specific position.
Also tried...
if (key.transform.Translate(0,6,0) == true)
{
Debug.Log("test");
}
but it doesn't seem to work either...
Also doesn't seem to work...
if(key.transform.Translate == Vector3(0,0,0) == true)
{
Debug.Log("test");
}
"gameObject.transform
" is not the same thing as "`gameObject`," and "`transform.position`" (a property / variable) is not the same thing as "`transform.Translate`". (a function / method) Those mean totally different things and are not interchangeable.
Similarly, using == twice in an if statement like you did shows that you don't understand what it means either. Actually I don't think you understand the syntax of C# or JS at all. That's ok, because everyone starts there. But rather than just guessing and hoping it will work out (it never will), you have to look up everything and read the reference manual very, very carefully before every word or symbol you type.
Also, what you're trying to do isn't something you should be doing, as robertbu suggested. The only reason a gameObject would be at 0,0,0 is because it was instantiated there and never moved. If there are a limited number of places it can be, you can store which set position as an integer (which can also be its index in your spawn point array) at the same time you originally placed it there. Or if it is moveable and you want to see if has been placed in a certain area, use a trigger collider.
Sorry, my answer had a problem. The above code should now compile and work.
Thanks @robertbu I am new (though I think you already know that). I got the script finally working. It seems that I messed up on one of my other scripts and things weren't working together correctly. However now thanks your guys assistance I manage to get the script fixed and functioning how I had hoped ^^
Your answer
Follow this Question
Related Questions
Issue's With Positioning 0 Answers
Game Object to Transform Position 1 Answer
Camera rotation around player while following. 6 Answers
Find a GameObject exactly in position Vector3(1,2,3)? 3 Answers
Camera movement 1 Answer