help with my script please

ok so…i have this sphere with tag “Player”…and i want to load another level whenever my sphere collides with another sphere…but when other thing collide with the sphere…it still loads the next level…for example…in oe of the levels i have a cube and whenever that cube collides with the other sphere…it loads the next level…:/…heres my script…please help i really need help with this! thanks -goatria

var levelToLoad: int;
var WaitTime = 3;

function Start(){
levelToLoad = Application.loadedLevel+1;
}

function OnTriggerEnter(hit : Collider){
if(hit.gameObject.tag == "Player"){
yield WaitForSeconds(WaitTime);
}
Application.LoadLevel(levelToLoad);
}

The if statement only executes what is in the brackets {}, and afterwards (true or false) continues to execute the program is normal.

The only thing inside the ‘if player’ brackets is the delay, it will still load level even if it is false.

Try:

var levelToLoad: int;
var WaitTime = 3;

function Start(){
levelToLoad = Application.loadedLevel+1;
}

function OnTriggerEnter(hit : Collider){
if(hit.gameObject.tag == "Player"){
yield WaitForSeconds(WaitTime);
Application.LoadLevel(levelToLoad);
}
}

Thanks For Helping!! But Nope…It Still Does it :/…Any Other Thing U Think It Might Be?..

i really need ur help guys! and i know this can be solved easily…thanks!

where did you attach this script? to the player or to the spheres where you will collide and load the level?

to the sphere i had to collide to…

Odds are you have another script somewhere that’s calling Application.LoadLevel.

Or every object has the Player tag

etc.

try this

var loadLever : boolean = false;
var levelToLoad: int;
var WaitTime = 3;

function Start(){
levelToLoad = Application.loadedLevel+1;
}

    function OnTriggerEnter(hit : Collider){
    if(hit.gameObject.tag == "Player")
    {
        yield WaitForSeconds(WaitTime);
       loadLevel = true;
      
        
    }
}
}

function Update()
{
   if(loadLevel == true)
   {

      Application.LoadLevel(levelToLoad);

    }
}

nope…didn’t work :/…idk what is it :/…its really annoying
thanks anyway

Try this:

var levelToLoad: int;
var WaitTime = 3;

function Start(){
levelToLoad = Application.loadedLevel+1;
}

function OnTriggerEnter(hit : Collider){
if(hit.gameObject.tag == "Player"){
yield WaitForSeconds(WaitTime);
Application.LoadLevel(levelToLoad);
} else {
return;
}
}

Maybe that’ll do the trick…

nope :/…ahhhh :(…thanks anyway

help please!!!

What’s the problem?

I know how hard it is to work with triggers. (It’s so buggy in Unity for some reason)

But maybe your problem is something simpler. Did you check to make sure your build settings have the scenes you want to load?

Its under File → Build Settings.

If it still doesn’t work, there are other means of starting the next level. (I’ll tell you but try this first and see if it works :P)

Let me get this straight, you want when a player hits an object for it to load a new level. But you also want an object that hits the player NOT to load a level?

Your further complicating it by putting a script on that object that says… “When ever I touch a player, load a level”

You can’t do both.

The only way to accomplish what you are saying is to test the relative speed between the objects. If the player has more relative speed, then load a level.

no…i want to load the next level…when the player hits with the sphere the script is attached to…but in my scene th is a cube that when it hits it it loads the level…and i don’t want that…i want for it to load ONLY when the player collides with it

OH, heh, no wonder…

This is why tabbing is a good practice in coding… Here is your script (reload is in red)

var levelToLoad: int;
var WaitTime = 3;

function Start(){
	levelToLoad = Application.loadedLevel+1;
}

function OnTriggerEnter(hit : Collider){
	if(hit.gameObject.tag == "Player"){
		yield WaitForSeconds(WaitTime);
	}
	[COLOR="red"]Application.LoadLevel(levelToLoad);[/COLOR]
}

here is your code fixed…

var levelToLoad: int;
var WaitTime = 3;

function Start(){
	levelToLoad = Application.loadedLevel+1;
}

function OnTriggerEnter(hit : Collider){
	if(hit.gameObject.tag == "Player"){
		//yield WaitForSeconds(WaitTime);
		[COLOR="red"]Application.LoadLevel(levelToLoad);[/COLOR]
	}
}

Now, I commented the yield, because you can’t do a yield in anything but a coroutine and the Start functions. So here is the code with a proper wait and load coroutine…

var levelToLoad: int;
var WaitTime = 3;

function Start(){
	levelToLoad = Application.loadedLevel+1;
}

function OnTriggerEnter(hit : Collider){
	if(hit.gameObject.tag == "Player"){
		StartCoroutine(WaitAndLoad(WaitTime));
	}
}

function WaitAndLoad(t){
	yield WaitForSeconds(t);
	Application.LoadLevel(levelToLoad);
}

AAAHHHH thank you so much!!!..finally it worked!!! im so happy!!! now i can continue with my project…thanks everyone that replyied but mostly thank you “BigMisterB” im so happy!!! XD…