I am trying to have all the colliders in a scene collide or touch each other at the same time in order to have it move to the next level but I keep getting the wrong result. For example, I need to have all the objects touching each other before the next level can be accessed. The problem I have is I need to have all the objects in the scene with the variable ‘stick’ to equal 2. But when some objects with the variable ‘stick’ equal 2, it moves to the next level. I need all the variables with ‘stick’ to equal 2.
Is there a way for me to check each variable in a scene at runtime? Or is there a way for me to search for the variable ‘stick’ and ‘swing’ at runtime? I need to have all the objects with the variables ‘swing’ == 2 and ‘stick’ == true before moving to the next scene.
You should not be doing this on TriggerStay. I wouldn’t use OnTriggerStay at all, since you have a lot of objects colliding probably.
You should adds the balls to a generic list when you Instantiate them. You can add the list on a script attached to the Main Camera or an empty GameObject. You can then check the list in a FOR loop every time a ball collides (OnTriggerEnter).
Something like:
import System.Collections.Generic;
var ballList : List.<GameObject> = new List.<GameObject>();
function CheckBalls()
{
for(var x =0; x < ballList .Count; x++)
{
var sticking : boolean = ballList.transform.GetComponent(yourBallScript).GetStatus()
if(!sticking ) //if there is a ball that is not sticking, stop checking and close the function
{
return;
}
}
//if there was no ball found that is not sticking, load the next level.
Application.LoadLevel(nextLevel);
}
//this on yourBallScript
function GetStatus()
{
return swing;
}
Thanks. Can you please send me the rest of the code? I would like to try it as well. So which function will i use to determine the collision on objects?
function OnCollisionEnter(col : collider)
{
swing = true;
camera.transform.GetComponent(WhateverScriptYouHave).CheckBalls();
}
function OnCollisionExit(col : collider)
{
swing = false;
}
You attach the script I gave you before to the camera. The script below you have to add to the script you arlready have for your balls:
var camera : GameObject;
function Start()
{
camera = GameObject.Find("Main Camera");
camera.transform.GetComponent(WhateverScriptYouHave).AddBall(this.gameObject);
}
Add this function to the script I gave you before:
function AddBall(ball : GameObject)
{
ballList.Add(ball);
}
Hey, after testing the code, this line, " var sticking : boolean = ballList.transform.GetComponent(yourBallScript).GetStatus()"
it is an error that says “BCE0019: ‘transform’ is not a member of ‘System.Collections.Generic.List.<UnityEngine.GameObject>’.” I tried to fix it but no luck.