Scene switching help

I have a simple scene switch script thanks to will goldstone :slight_smile: but cannot figure out a way to make it work the way i want it to work.

var myLevel : String;

function OnCollisionEnter (myCollision : Collision) 
{
	if(myCollision.gameObject.name == "endlevel")
	{
		Application.LoadLevel(myLevel);
	}	
}

I have two objects that are controlled at the same time with the same keys and i want the scene to switch when they are both at their target destination, it doesn’t matter in what order they get there.

I’ve crafted a little picture to help explain what i mean :slight_smile: the objects would start on the left and when they are both within the red areas on the right then the scene would switch.
Any advice :)?

Maybe have a variable that counts up once the object reaches the goal and load the next level then.

Have the objects know whether they’re in their zone or not, and have the game control logic advance the level if all blocks return that they are in their zone.

Thanks for the quick replies, uhm… i’m not sure how i would go about scripting either of those suggestion, could anyone give me some direction ?

have an object know whether or not
boolean.
it’s in the zone
on trigger enter
and have the game control logic
global controlling script
if all blocks return
global control needs to ask the blocks about their status.

Block.js

var inEndZone : boolean = false;
EndZone.js - attach to trigger collider

function OnTriggerEnter( other : Collider ) {
 if ( other.GetComponent(Block) ) {
  other.GetComponent(Block).inEndZone = true;
 }
}
function OnTriggerExit( other : Collider ) {
 if ( other.GetComponent(Block) ) {
  other.GetComponent(Block).inEndZone = false;
 }
Controller.js - attach to any one object

var allBlocks : Array;

function Start() {
 allBlocks = GameObject.FindObjectsOfType( Block );
}

function Update() {
 var blocksMissing : int = 0;
 for ( var b : Block in allBlocks ) {
  if ( !b.inEndZone ) blocksMissing += 1;
 }
 if ( blocksMissing == 0 ) Win();
}

Thank you very much for those scripts :smile:

I’ve been trying to get it all working and did a small amount of editing to one of the scripts but seem to be getting an error.

var allBlocks : Array;
var myLevel : String;

function Start() {
 allBlocks = GameObject.FindObjectsOfType( Block );
}

function Update() {
 var blocksMissing : int = 0;
 for ( var b : Block in allBlocks ) {
  if ( !b.inEndZone ) blocksMissing += 1;
 }
 if ( blocksMissing == 0 )
{
     Application.LoadLevel(myLevel);
}

From my very basic understanding of scripting i think the script is correct but the console keeps giving me this error

expecting }, found ā€˜ā€™.

and it’s referring to the last } which already there, i can’t remember it’s name :stuck_out_tongue:

{ curly braces } is the best name I know for them. :slight_smile:

Your Update function has no closing brace. You can put the typing cursor on a brace and its matching buddy will be highlighted; if you do that with Update’s open brace you’ll see there’s no match.

I sorted out the script but i still can’t get it all to work, i attached the Controller script to one of the spheres i’m controlling, the EndZone to two triggers and the Block script to a random object that i named Block, when i check the box on the Block all is does it make the scene i’ve chosen start when i press the play button and when it’s not check nothing happens :stuck_out_tongue:

I could be doing something completely wrong but i’m really not sure, sorry to be a further pain :stuck_out_tongue:

Block.js should be attached to all the objects that need to wind up in an End Zone. I think I was mixing a couple questions in my head and though you specifically had blocks in yours. Obviously it’ll do you good to rename it to something more appropriate like ā€œsphereā€. :slight_smile:

:open_mouth: thank you soo very much, now that i have put all the scripts in the right place it all works perfectly :slight_smile: still not 100% sure on exatly how the scripts work but i’m sure with experimentation i’ll soon get a firm grasp on it, thanks again and i hope i can help you out at some point.