- Home /
Game Objects enable problems
Hello, i have an 'if' condition and when it is true , i am trying to activate a game object and to enable a Script component on the same game object, and this is my code :
var building7 : GameObject;
building7.active = false;
var target :Transform;
if (distance>7 && target.position.x < -32)
{
building7.active = true;
var findingbuilding7 : findingbuilding7 = building7.GetComponent("findingbuilding7");
findingbuilding7.enabled = true;
}
and the target is the main camera , and the code is attached to the main camera ,when i hit play the building dosen't activate ,but the script component enable.
i really can't figure out what i am doing wrong!!
thanks in advance for your help.
P.S : Sorry for my bad english
try to check if ur IF statement is working try to print something. is the script attached to the camera?
i tried to print something Debug.log ("ok") , it is not working with the same condition , but when i change the condition to only : if (distance>7) it is working just fine ,
i want the Gameobject to be activated when the distance is bigger than 7 , and when the gameobject is in the field view of the camera thats why i added the condition 'target.position.x < -32 ' ....
and yes this script is attached to the camera.
thank you for your reply :)
Your na$$anonymous$$g conventions are all the same. You have a variable, class name and component all named the same. Thats bad form. Always name everything different, its helps to distinguish what you are using and it helps the editor from being confused.
You look like you are trying to turn on an object when the statement meets requirements. building7.active = true; might work better if you use var building7 : Renderer;. When you are making an object show and appear, you are manipulating the renderer object. This is the object that carries the mesh renderer. When you call a GameObject, you are calling for the whole piece, which doesnt let the editor find exactly what you are looking for.
Another thing. I would say to look for a GameObject and its component in a Start() or Awake(). It only needs to be called once to locate the object and their scripts for reference. From there you just call whatever scripts that are attached through the objectVariable.ExecuteWhatYouWant().
$$anonymous$$ake sense?
first of all thank you for your advices , i am new to programmation and this helps me a lot .. :)
for turning on the game object , i tried building6.GetComponent($$anonymous$$eshRenderer).enabled = true; and it also didnt work. and i called the game object on Start() function.
i think that my problem is with the condition , target.position.x < -32 , because when i took this condition off evrey thing was working fine , is there another way to make sure that the building only activate if it is in the camera filed of view
Your English is great !
$$anonymous$$uch better and more careful than 50% of native English writers.
Your answer
Follow this Question
Related Questions
How to enable children of child game object? 1 Answer
How to disable an array of projectors 2 Answers
enable scripts/ components on other gameobjects for a different gameobject? 1 Answer
Help With Disabling/Enabling single GameObject 1 Answer
Using raycast to "collide" with tagged objects & enable Halo Component 2 Answers