Help with collisions (Java)

I’m just messing around with collisions with unity, so at the moment I’ve got it so when the player enters a certain area of the level they gain 10 experience and when they reach 100 experience points a notification pops up saying they have leveled up. When I try to run it in game it has an error that says "Expressions in statements must only be executed for their side effects, sorry if it’s something simple I’m pretty new to scripting :slight_smile:

var detectcube : boolean = false;
var experience : int = 0;


function OnTriggerEnter() {
   detectcube = true;
}
function OnTriggerExit() {
   detectcube = false;
}

function OnGUI()
{ 
   if (detectcube)
   {
     experience + 10;
   }   
    if(experience > 100)
  {
      GUI.Box  (Rect(10,10,200,50),"You have levelled up!");
  }
    else
  {
      GUI.Label (Rect(10,10,0,0),"");
  }
}

Try this:

var experience : int = 0;
 
function OnTriggerEnter() {
 experience += 10;
}

function OnGUI()
{ 
    if(experience > 100){
      GUI.Box  (Rect(10,10,200,50),"You have leveled up!");
  } 
}

I don’t use UnityScript so I’m not 100% sure what is causing the error, but the code that I posted “should” work.

You forgot to put a “=”. It should look something like this :

 if (detectcube)

   {

     experience += 10;

   }

But remember that function OnGUI is called every frame so it will add 10 Experience Points every frame that you’re in the trigger. You should go with what Sildaekar says since OnTriggerEnter is called once.

Thanks for the help it works now :smile: