- Home /
Increase score when hit by a prefab help?
Currently i have this script:
function OnCollisionEnter(theCollision :
Collision){
if(theCollision.gameObject.name == "Sphere1(Clone)"){
ScoreControl.score += 100;
Debug.Log("Avoid the spheres!");
if(theCollision.gameObject.name == "Sphere2(Clone)"){
ScoreControl.score += 100;
Debug.Log("Avoid the spheres!");
if(theCollision.gameObject.name == "Sphere3(Clone)"){
ScoreControl.score += 100;
Debug.Log("Avoid the spheres!");
if(theCollision.gameObject.name == "Sphere4(Clone)"){
ScoreControl.score += 100;
Debug.Log("Avoid the spheres!");
if(theCollision.gameObject.name == "Sphere5(Clone)"){
ScoreControl.score += 100;
Debug.Log("Avoid the spheres!");
if(theCollision.gameObject.FindWithTag
("bullet")) {
ScoreControl.score += 150;
Debug.Log("Don't get hit!");
}
}
}
}
}
}
}
The last part works fine, but i am having trouble with my sphere prefabs.
Basically, they are rigid bodies that are spawned from gameobjects and they roll down a hill, what i want to happen, is when they hit my player character, for the score to increase to the above amount 100 points, but for some reason it is not working, why is this?
I made them prefabs, even tried checking "collision detection: dynamic continuous" but every time my character gets hit by the spheres nothing happens, this is the script i have attached to the player character obviously too.
What do i need to do to make it so the score increases when im hit by the spheres?
Thanks in advance.
Answer by Anxo · Jan 07, 2012 at 04:37 PM
you are not closing your if statements in the correct place.
if(theCollision.gameObject.name == "Sphere1(Clone)"){
ScoreControl.score += 100;
Debug.Log("Avoid the spheres!");
} <------- "this is an arrow"
meant so say here that, thanks for this but it still doesnt work :S, spheres continue to roll down and hit my character without adding points, i have a roothandle and collision box on my character as its a fully animated model...does this make a difference? should i attach the script in more than one place? This is how it looks now:
http://pastebin.com/Cn9BYhg$$anonymous$$
I dont understand how to get this to work :S, i thought it would be simple lol.
Well first, you are using the wrong function to compare tag. You are using gameObject.FindWithTag ins$$anonymous$$d of theCollision.gameObject.CompareTag("tag")
You also have to make sure that the var you are trying to access in the score control script is a static var.
It is a really simple thing to do and ins$$anonymous$$d of trying to make it work for 2 days, I highly recommend that you run through these $$anonymous$$odules first and then try again. You will be able to do it in now time and you will be able to do a lot of other things very fast.
Answer by Pixelen · Jan 08, 2012 at 02:52 PM
I had this problem too, and what I did was create a separate script for scores, so in the script attached to the player you should put:
if(theCollision.gameObject.name == "Sphere2(Clone)"){
gameObject.Find("Score").SendMessage("ScoreUp"); //this calls the score function
Debug.Log("Avoid the spheres!");
}
And then you should create a different script and attach it to the score GUI, something like:
var counter : int = 0;
function Update () {
guiText.text = "Score: "+counter;
}
function ScoreUp () {
counter+=100;
}
Hope this helps!
Thanks for this but nope...still doesnt work :(. I added that script to the score GUI and i added that top script to the player character and nothing...i just dont understand why it would detect the spheres on the player character...
I currently have these 4 scripts:
1: attached to my "Player" : http://pastebin.com/TbJdhUDv 2: attacked to the "score" gui element : http://pastebin.com/caNkdjEY 3: attached to my "Player" (this works) : http://pastebin.com/DezULShg 4: attack to the "score" gui element : http://pastebin.com/LeEsBLhB
So two seperate scripts each and no luck...anymore ideas?
If not ill just give up lol.
Thanks all so far.