Scripting Help: Score operations and ball respawn

HI all !!! i’m totally new to unity and scripting so i’m having some problems wich i think are very basic level…

Well first: I have i Gui text with the score and would like to have a script that reads the score and increases the pitch of a AudioFile if the score is more than a certain number…

Second: if a certain object is hit, after a 5 second wait a particular object in the game should be placed back to his starting position

Third: I use the marble tutorial script for the score,

var score : Score; 
function Update () { 
	
	

} 
function OnCollisionEnter (collision : Collision) { 
 		

 

 score.AddToScore (); 
 
	

 }

is it possible to have differrent scores sent to the score gui for different objects hit??? Essentially can i change the score value this script sends to the the one on the GuiText ???

Thanks a lot just for reading my more than ignorant questions
Hope for Help…

I’m not sure about what you want to do, but as far as I understand you just want to set the GUI Text score value. If this is what you want then you can extend the “Score”-script with a set-function.

var score = 0; 
function AddToScore() { 
 ++score; 
 guiText.text = "Score: " + score.ToString();	
}

function SetScore(score_value) {
	guiText.text = "Score: " + score_value.ToString();
}

Then you can call this SetScore function from anywhere else.

score.SetScore(12);

Something like this?

Thanks a lot for the fast reply!!!

Actually what I need is the opposite , I should be able to read the value of the score from another script, and if score value is more than 50 do something

This is even easier. You need a get-function. Extend the Score-script with:

var score = 0; 
function AddToScore() { 
 ++score; 
 guiText.text = "Score: " + score.ToString();    
} 

function GetScore() { 
   return score; 
}

Thanks Again,
but then now what do I have to put in another script, to access score and if > 10 play sound ???
Sorry but I really now nothing about coding… tryng to learn but…

Try these:

ScoreCounter.js

static var score = 0;
function AddToScore() {
 ++score;
 guiText.text = "Score: " + score.ToString();   
}

PlaySound.js

function Update() {
   if (ScoreCounter.score > 10) {
      audio.play();
   }
}

The word static in the first script means you can reference that variable from any other script by starting with the original scripts name. :)[/code]

Ball respawn is easy too …

Just call this script Respawn.js and attach it to each of the objects you want to respawn … in this case, when you hit the space bar anything with this attached will reset to its starting position …

var respawnPosition: Vector3; 

function Awake() { 
   // this saves the objects original position 
   respawnPosition=transform.position; 
} 

function Update () { 
   if (Input.GetButton ("Jump")) { 
      transform.position = respawnPosition; 
   }    
}

It creates a variable named “respawnPosition” that is a 3D position. It notes this when the level starts and then function Update listens every frame for the space bar being pressed. If it detects the spacebar it transforms the positon of the object back to the start.

You can of course replace the spacebar condition with any other … losing a life, falling below a certain Y height, etc.

var respawnPosition: Vector3; 

function Awake() { 
   // this saves the objects original position 
   respawnPosition=transform.position; 
} 

function Update () { 
   if ("my condition is met") { 
      transform.position = respawnPosition; 
   }    
}

In this case, if the object falls down below a certain Y value (i.e., off a platform into space) it’ll reset …

var respawnPosition: Vector3; 

function Awake() { 
   // this saves the objects original position 
   respawnPosition=transform.position; 
} 

function Update () { 
   if (transform.position.y < -35) { 
      transform.position = respawnPosition; 
   }    
}

Hope that helps.

Thanks to all!!! now my scoring prolbems are solved!

var respawnPosition: Vector3; 

function Awake() { 
   // this saves the objects original position 
   respawnPosition=transform.position; 
} 

function Update () { 
   if (Input.GetButton ("Jump")) { 
      transform.position = respawnPosition; 
   }    
}

great thanks!!!

Do, how do i tell him to respawn if it hits a certain object?
and, is there a way to tell him to wait before respawn?

Real thanks to eveybody, the unity community is incredible!!!

Now don’t quote me on this and some other 15 year old genius will toodle along soon to write this correctly but here it is commented …

var respawnPosition: Vector3;

function Awake() {
   // this saves the objects original position
   respawnPosition=transform.position;
}

"if a collision happens" () {
      yield WaitForSeconds (5);
      transform.position = respawnPosition;
}

Something like that. I’ll move aside now for a real coder to write it correctly … :wink:

That’s the idea, just this:

"if a collision happens" () {
      yield WaitForSeconds (5);
      transform.position = respawnPosition;
}

becomes

functionOnCollisionEnter(collision : Collision) {
      if( collision.gameObject.name == "name of certain object" )
      {
            yield WaitForSeconds (5);
            transform.position = respawnPosition;
      }
}

Also, if you want to add tags to the “certain object” you can check for them by checking for collision.gameObject.tag in the if statement.

I’m using this code :

var respawnPosition: Vector3; 

function Awake() { 
   // this saves the objects original position 
   respawnPosition=transform.position; 
} 
functionOnCollisionEnter(collision : Collision) { 
      if( collision.gameObject.name == "Gol" ) 
      {
            yield WaitForSeconds (5); 
            transform.position = respawnPosition; 
            }
}

have assigned it to the ball i want to respawn, but it has an error at line 12, it says EOF expected found } …

You need a space after “function” in the second snippet…
AC

Now everything is working fine!!!

Great Thanks to all