I have a simple scene switch script thanks to will goldstone 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 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 :)?
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.
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();
}
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
{ curly braces } is the best name I know for them.
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
I could be doing something completely wrong but iām really not sure, sorry to be a further pain
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ā.
thank you soo very much, now that i have put all the scripts in the right place it all works perfectly 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.