- Home /
Collection quest trouble. JavaScript
Hey guys and girls
Just asking a question in regards to a sort of quest system in JavaScript I Currently have two objects Sphere 1 and Sphere 2 these two spheres both have colliders around them as shown below, I want the player to walk through both colliders then set both the spheres to SetActive = false easy enough, but then I want it too check when the player walks into a collider if the other object is false, so if I walk into sphere 1 then check to see if sphere 2 is SetActive = false if the sphere reads as false, turn on another collider(not shown in the image)
this is what I am currently using, two scripts, one on each collider around Sphere1 and Sphere2
var collider1 : GameObject;
var Sphere1 : GameObject;
function OnTriggerEnter ()
{
Sphere1.SetActive (false);
if(GameObject.FindWithTag("Sphere2").SetActive == false)
{
collider1.SetActive (true);
}
}
var collider1 : GameObject;
var Sphere2 : GameObject;
function OnTriggerEnter ()
{
Sphere2.SetActive (false);
if(GameObject.FindWithTag("Sphere1").SetActive == false)
{
collider1.SetActive (true);
}
}
not sure where I am going wrong but any help would be greatly appreciated
thanks!
Answer by HarshadK · Oct 09, 2014 at 11:18 AM
You need to check if the game object is active or not using GameObject.activeInHierarchy.
So your if condition becomes:
if(GameObject.FindWithTag("Sphere2").activeInHierarchy == false)
and same for another game object.
But since if the game object is inactive then GameObject.FindWithTag will not be able to find it anyways. You need to cache a reference to the required game object beforehand.
For this your script becomes:
Script on Collider 1:
var collider1 : GameObject;
var Sphere1 : GameObject;
var Sphere2 : GameObject;
function Start()
{
// You can use this or you can assign it directly in inspector. You should assign it in inspector if this game object is going to be inactive at start.
Sphere2 = GameObject.FindWithTag("Sphere");
}
function OnTriggerEnter ()
{
Sphere1.SetActive (false);
if(Sphere2.activeInHierarchy == false)
{
collider1.SetActive (true);
}
}
Similarly you need to do for second colllider.
awesome thanks for your reply I tried this out today an for some reason It runs the start function but when I enter the second trigger It doesn't get past the If statement still,
var collider1 : GameObject;
var collider2 : GameObject;
var Heart : GameObject;
var Feather : GameObject;
function Start()
{
// You can use this or you can assign it directly in inspector.
Heart = GameObject.FindWithTag("Heart");
Debug.Log ("StartFuncWorking");
}
function OnTriggerEnter ()
{
Feather.SetActive (false);
if(Heart.SetActive == false)
{
collider1.SetActive (true);
collider2.SetActive (true);
Debug.Log ("heart and feather found");
}
}
this is my updated script, It has changed a bit to suit the purpose in the game. any ideas? I walk into one collider, then the second one and the objects itself gets turned off but still nothing after that happens
You should check if object is active using GameObject.activeInHierarchy.
So your if statement becomes:
if(Heart.activeInHierarchy == false) {
collider1.SetActive (true);
collider2.SetActive (true);
Debug.Log ("heart and feather found");
}
ahh okay I put my if statement as
if(GameObject.FindWithTag("Sphere2").activeInHierarchy == false)
and it wasn't working that makes more sense
thanks again!