Hello, i have a problem.
I’m trying to create a simple game in Unity in which the character has to perform a gesture with his arm. I want to create the gestures with multiple collisions in a specific order. At the moment I have 5 spheres in a row which i want the character to activate from left to right. I tried to put booleans on every sphere and when the character collides with the sphere the boolean will be set to true. When enough booleans are set to true (in our case 3) the character has performed the gesture good enough.
In the beginning all booleans are set to false but when they are set true they turn back immediately.
This is our code:
` var aSound: AudioClip;
var Sphere1 : boolean = false;
var Sphere2 : boolean = false;
var Sphere3 : boolean = false;
var Sphere4 : boolean = false;
var Sphere5 : boolean = false;
var aantalGeraakt : int;
function OnCollisionExit(theCollision : Collision){
if(gameObject.name == "Sphere1" && theCollision.gameObject.tag == "Rechts"){
Debug.Log("Hit Sphere1");
audio.PlayOneShot(aSound);
Sphere1 = true;
aantalGeraakt ++;
}
if(gameObject.name == "Sphere2" && theCollision.gameObject.tag == "Rechts"){
Debug.Log("Hit Sphere2");
audio.PlayOneShot(aSound);
Sphere2 = true;
aantalGeraakt ++;
}
if(gameObject.name == "Sphere3" && theCollision.gameObject.tag == "Rechts"){
Debug.Log("Hit Sphere3");
audio.PlayOneShot(aSound);
Sphere3 = true;
aantalGeraakt ++;
}
if(gameObject.name == "Sphere4" && theCollision.gameObject.tag == "Rechts"){
Debug.Log("Hit Sphere4");
audio.PlayOneShot(aSound);
Sphere4 = true;
aantalGeraakt ++;
}
if(gameObject.name == "Sphere5" && theCollision.gameObject.tag == "Rechts"){
Debug.Log("Hit Sphere5");
audio.PlayOneShot(aSound);
Sphere5 = true;
aantalGeraakt ++;
}
Debug.Log(aantalGeraakt);
Debug.Log("blokkie 2: " +Sphere2+ " - " + Sphere3);
return;
}`
I have the feeling that the whole script is in some sort of loop which resets all variables back to default.
Who knows how to solve this problem?
Thanks in advance
this code is attached to several gameObjects, right?
well in each script there will be different values for each variable…
So it means that the last script will write in console its contents of variables… I would prefer to use one script with a link to each gameObject, that will make your job easier and you will get correct boolean values.
var spheres: GameObject[];//set size to 5 and add all spheres
var aantalGeraakt : int;
function OnCollisionExit(theCollision : Collision)
{
for(var i=0;i<spheres.length;i++)
{
if(spheres*==theCollision.gameObject&&theCollision.gameObject.tag == "Rechts")*
{
audio.PlayOneShot(aSound);
aantalGeraakt ++;
}
}
}
first of all add script to empty object (at least not on sphere), then
var spheres : GameObject[];
var aantalGeraakt : int;
var aSound : AudioClip;
function OnCollisionExit(theCollision: Collision)
{
for(var i=i;i<spheres.length;i++)
{
if(spheres*==theCollision.gameObject)//check if the collision gameObject matches one of spheres*
{
print(Sphere: +spheres*.name);//print which sphere got collision*
audio.PlayOneShot(aSound);
aantalGeraakt++; //no need to increase index, because its already done in loop
}
}
}
If i understood corectly, u want to check if there is a collision with sphere and so u want to get which one collides
Cheers!
i got an arror, sorry
var spheres : GameObject[];
var aantalGeraakt : int;
var aSound : AudioClip;
function OnCollisionExit(theCollision: Collision)
{
for(var i=0;i<spheres.length;i++)
{
if(spheres*==theCollision.gameObject)//check if the collision gameObject matches one of spheres*
{
print(Sphere: +spheres*.name);//print which sphere got collision*
audio.PlayOneShot(aSound);
aantalGeraakt++; //no need to increase index, because its already done in loop
}
}
}
i got an arror, sorry
var spheres : GameObject[];
var aantalGeraakt : int;
var aSound : AudioClip;
function OnCollisionExit(theCollision: Collision)
{
for(var i=0;i<spheres.length;i++)
{
if(spheres*==theCollision.gameObject)//check if the collision gameObject matches one of spheres*
{
print(Sphere: +spheres*.name);//print which sphere got collision*
audio.PlayOneShot(aSound);
aantalGeraakt++; //no need to increase index, because its already done in loop
}
}
}