- Home /
Why my game just win all the way?
Assume that i have 2 cube as the game object named cube1 and cube2 and 2 transparency cube as collider named co1 and co2 when the cube1 hit the co1 && cube2 hit the co2 = win game
Here's My code of my MainScript
var Cube1: GameObject;
var Cube2: GameObject;
function Update()
{
Cube1 = GameObject.Find ("Cube_1");
Cube2 = GameObject.Find ("Cube_2");
}
function FixedUpdate()
{
if (C1.Collided == true && C2.Collided == true)
{
Application.LoadLevel("Level_2")
}
}
This is the boolean script for co1 and co2, another 1 assigned to co2 just change the cube_1 to cube_2
static var Collided = false;
function OnTriggerEnter (col : Collider)
{
if(col.name == "Cube_1")
{
Collided = true;
print ("Hit Cube_1");
}
}
function OnTriggerExit (col : Collider)
{
if (col.name == "Cube_1")
{
Collided = false;
print ("Cube_1 Left");
}
}
So now here's the problem, when i play my Level 1, everything goes fine, when i load to level 2 and my cube1 and cube2 haven hit the co1 and co2, it just win the game straight away and load to level 3. But if i direct jump to level 2,everything works fine, This problem happens in other levels as well, can anyone help me?
P/S: Sorry for my broken english
Thank You!
col.gameObject.name makes more sense. The collider is nameless.
You haven't posted the go to level 3 code.
so i just need to change my game object name in level 2, 3 and so on?
i though if we want to call function from another script we need to use static? because the error keep saying cannot reference to non-static member
the code go to level 3 just almost same with the code go to level 2
Answer by tanoshimi · Nov 26, 2014 at 11:08 PM
col.name is fine. All components share the same name as the gameobject to which they are attached.
http://docs.unity3d.com/ScriptReference/Collider.html http://docs.unity3d.com/ScriptReference/Object-name.html
I suspect this due to the use of static variables (why?). You say you attach he same script to each cube, so the class-level static variable "Collided" will be being shared between both cubes.
Hi tanoshimi, sorry for bothering you. Is there any solution for me to reset my game after i clicked restart button? Cause it just win straight away after i clicked restart level 1. I don't want to change the gameObject name, is there any solution for me to reset the game?
After i change my static var to public and use getcomponent in my main script. i get this error >> NullReferenceException, can you please help me?
Your answer