- Home /
Unlockable Levels - Object Masking
Hello. This is my first post so plz excuse any mistakes. So, I have been trying to develop an Unlockable Level System. I've made lots of scripts but I can't seem to find out why it doesn't work. Here is what I have:
This loads the level at the level menu and then sets the room number for the try again feature.
Levelselecter.js
public static var room: int = 1;
var particle : GameObject;
var rayCastHit : RaycastHit;
var ray : Ray;
function Update () {
if(Input.GetMouseButtonDown(0))
{
ray = Camera.main.ScreenPointToRay (Input.mousePosition);
if (Physics.Raycast (ray, rayCastHit))
{
if(rayCastHit.transform.name == "Cos1")
{
room =1;
Application.LoadLevel("Cosmos1");
}
else if(rayCastHit.transform.name == "Cos2")
{
room =2;
Application.LoadLevel("Cosmos2");
}
else if(rayCastHit.transform.name == "Cos3")
{
Application.LoadLevel("Cosmos3");
room = 3;
}
//ETC to 20
Next, when the ball in the Level hits the portal to get to win the level it checks if the level the player was on was won for the first time (by making the UCosmos (Unlockedlevels variable) increased if the unlocked level is = to the room) and then sending the player back to the level menu to see the new unlocked level:
Ball.js
var Crackle : GameObject;
var Pong : AudioClip;
public static var UCosmos: int = 1; //UCosmos is the variable that saves the number of Unlocked Levels in my pack Cosmos
function Awake()
{
rigidbody.AddForce(0, -4, 0, ForceMode.Impulse);
}
function Update(){
if(transform.position.y < -5){
Application.LoadLevel("TryAgain");
}}
function OnCollisionEnter(theCollision : Collision){
if(theCollision.gameObject.name == "Portal Cosmos"){
rigidbody.velocity=Vector3.zero;
rigidbody.angularVelocity=Vector3.zero;
if(Levelselecter.room==1){
if(UCosmos == 1){
UCosmos += 1;
}}
else if(Levelselecter.room==19){
if(UCosmos == 19){
UCosmos += 1;
}}
else if(Levelselecter.room==18){
if(UCosmos == 18){
UCosmos += 1;
}}
else if(Levelselecter.room==17){
if(UCosmos == 17){
UCosmos += 1;
}}
else if(Levelselecter.room==16){
if(UCosmos == 16){
UCosmos += 1;
}}
else if(Levelselecter.room==15){
if(UCosmos == 15){
UCosmos += 1;
}}
else if(Levelselecter.room==14){
if(UCosmos == 14){
UCosmos += 1;
}}
else if(Levelselecter.room==13){
if(UCosmos == 13){
UCosmos += 1;
}}
else if(Levelselecter.room==12){
if(UCosmos == 12){
UCosmos += 1;
}}
else if(Levelselecter.room==11){
if(UCosmos == 11){
UCosmos += 1;
}}
else if(Levelselecter.room==10){
if(UCosmos == 10){
UCosmos += 1;
}}
else if(Levelselecter.room==9){
if(UCosmos == 9){
UCosmos += 1;
}}
else if(Levelselecter.room==8){
if(UCosmos == 8){
UCosmos += 1;
}}
else if(Levelselecter.room==7){
if(UCosmos == 7){
UCosmos += 1;
}}
else if(Levelselecter.room==6){
if(UCosmos == 6){
UCosmos += 1;
}}
else if(Levelselecter.room==5){
if(UCosmos == 5){
UCosmos += 1;
}}
else if(Levelselecter.room==4){
if(UCosmos == 4){
UCosmos += 1;
}}
else if(Levelselecter.room==3){
if(UCosmos == 3){
UCosmos += 1;
}}
else if(Levelselecter.room==2){
if(UCosmos == 2){
UCosmos += 1;
}
}
else if(Levelselecter.room==20){
if(UCosmos == 20){
UCosmos += 1;
}
}
Application.LoadLevel("Cosmos Levels");
}
audio.Play();
Instantiate (Crackle, transform.position, transform.rotation);
}`
So on the level selection page, there are all the level boxes. On everyone but lv 1, there is a black box called Cost.. (Cost2 goes on Cos2,etc). When the level was completed, I tries making it so that the UCosmos destroys the Cost blocks like so:
var Cost20 : GameObject;
var Cost19 : GameObject;
var Cost18 : GameObject;
var Cost17 : GameObject;
var Cost16 : GameObject;
var Cost15 : GameObject;
var Cost14 : GameObject;
var Cost13 : GameObject;
var Cost12 : GameObject;
var Cost11 : GameObject;
var Cost10 : GameObject;
var Cost9 : GameObject;
var Cost8 : GameObject;
var Cost7 : GameObject;
var Cost6 : GameObject;
var Cost5 : GameObject;
var Cost4 : GameObject;
var Cost3 : GameObject;
var Cost2 : GameObject;
function Update(){
if(Ball.UCosmos == 2){
Destroy(Cost2);
}
else if(Ball.UCosmos == 3){
Destroy(Cost3);
}
else if(Ball.UCosmos == 4)
{Destroy(Cost4);}
else if(Ball.UCosmos >= 5)
{Destroy(Cost5);}
else if(Ball.UCosmos >= 6)
{Destroy(Cost6);}
else if(Ball.UCosmos >= 7)
{Destroy(Cost7);}
else if(Ball.UCosmos >= 8)
{Destroy(Cost8);}
else if(Ball.UCosmos >= 9)
{Destroy(Cost9);}
else if(Ball.UCosmos >= 10)
{Destroy(Cost10);}
else if(Ball.UCosmos >= 11)
{Destroy(Cost11);}
else if(Ball.UCosmos >= 12)
{Destroy(Cost12);}
else if(Ball.UCosmos >= 13)
{Destroy(Cost13);}
else if(Ball.UCosmos >= 14)
{Destroy(Cost14);}
else if(Ball.UCosmos >= 15)
{Destroy(Cost15);}
else if(Ball.UCosmos >= 16)
{Destroy(Cost16);}
else if(Ball.UCosmos >= 17)
{Destroy(Cost17);}
else if(Ball.UCosmos >= 18)
{Destroy(Cost18);}
else if(Ball.UCosmos >= 19)
{Destroy(Cost19);}
else if(Ball.UCosmos >= 20)
{Destroy(Cost20);}
}
The Problem is that it doesn't destroy the Cost blocks. Help?!?!??!?
BTW, there are no errors that it says in the system so thats why I don't know how to fix it.
That's pretty impressive for a 13 years old. :-) I didn't even had a computer back then when I was 13. :-(
Now to the actual problem.
you should get a reference to the Ball Script using (Considering both these scripts are on same game object):
var ball : Ball = gameObject.getComponent("Ball") as Ball;
And change your every Ball.UCosmos to ball.UCosmos.
Also don't make your UCosmos variable in Ball.js static variable.
See if this solves your issue.
You create variables of type GameObject in your script but these do not link to any objects in the hierarchy. These internal objects will become destroyed just fine.
One way to solve this problem is to have the hierarchy GameObject destroy itself when the condition is met.