- Home /
If gameobject moves do this
I am trying to have one gameobject set active upon another gameobject moving beyond its current position in the y axis. Please help if you can. Getting a "Missing Field Exception: System.Boolean.transform" error. Whatever that means. See code below. Please help. Thanks.
function Update (){ if ((gameObject.tag == "door").transform.position.y > 1){ doorlight.SetActive(true); } }
For this and future posts, please use the 101010 button to format your code.
You are taking a Boolean condition if(condition) and trying to tack on a .transform.position which isn't valid syntax. Refactor that.
I suggest learning how to code properly first.. try this http://unity3d.com/learn/tutorials/modules/beginner/scripting
Answer by RudyTheDev · Feb 23, 2014 at 09:20 PM
What you need to do is very roughly:
public class YourClass : MonoBehaviour
{
public GameObject doorlight; // drag&drop this in inspector
private GameObject door; // we will find this (or could make it public and also drag&drop in inspector, then Start() isn't needed)
function Start()
{
door = GameObject.FindWithTag("door"); // find the door - game object with door tag
}
function Update()
{
if (door.transform.position.y > 1)
{
doorlight.SetActive(true);
}
}
}
Question is, how you actually want to define, initialize and access your door
. You gave very little detail about your game and what door or light are and how you access them.
Yea, I am learning. This is all slowly starting to make sense. It seems as though you were writing in "C", and not Javascript but since they seem so similar I was able to figure out where I was wrong based on what you provided. You were super helpful. Thank you so much!!!
I needed to make a door variable, which I didn't have, and assign my game object "door" to it in the inspector. Have it accessed in the start function as you showed me, then have the update function do its thing with if (door means this) {then do this}. Also needed to tweak the y units from 1 to 50 to be effective. I already had a separate script moving the door.
Again Thank you. Very helpful.
@mgadth: Oh, whoops, my bad. I copy-pasted and missed the function
while the rest was the same for both languages. I guess $$anonymous$$e is pseudo-code then :) Glad it at least helped. P.S. You can mark the answer as accepted to "close" the question.
Thanks! I'm new at the forum too. The tutorial is not functioning, lol.
Your answer
Follow this Question
Related Questions
stop object(door) translation 0 Answers
C# the position of the object 1 Answer
Freeze transform.rotation.x 1 Answer