- Home /
Scoring System
I have been looking at this script for hours and have gotten nowhere. I have two scripts. One manages the score ad is on the gameManager. The second is on the enemy. I cannot figure out what is wrong.
Score Script
#pragma strict
var curPoints : int;
function Start () {
}
function ApplyPoints (curPoints : int) {
Debug.Log(curPoints);
GameObject.Find("Scoregui").guiText.text = ""+ curPoints;
}
Enemy Script
#pragma strict
var gameManager : GameObject;
function OnCollisionEnter (collision : Collision)
{
gameManager.GetComponent("Score");
Component("Score").SendMessage("ApplyPoints", 10);
Destroy(gameObject);
}
Answer by FirePlantGames · Dec 16, 2013 at 01:56 AM
first of all get rid of the pragama script at the top
then use this for the enemy: var gameManager : GameObject;
function Awake()
{
//so you don't have to assign the game manager in the
//inspector manually(also have the manager tagged as player)
gameManager = GameObject.FindWithTag("Player");
}
function OnCollisionEnter(col : Collision)
{
//i am assuming your controller script is called
//Score
if (col.gameObject == gameManager)
{
gameManager.GetComponent(Score).curPoints += 1;
Destroy(gameObject);
}
}
sorry have the: var game$$anonymous$$anager : GameObject; in the script
You should not get rid of #pragma strict if you care about optimizations. Please, don't say that he should get rid of that as a fact.
Answer by YoungDeveloper · Dec 16, 2013 at 01:51 AM
I don't understand why do you need OnCollisionEnter if you don't use it, but instead manually adding the gameobject in inspector. And there is really no need to use sendmessage, as it is slower.
Enemy Script
function OnCollisionEnter (col : Collision){
var scoreScript : Score = col.GetComponent("Score");
scoreScript.ApplyPoints(10);
Destroy(gameObject);
}
Score Script
var curPoints : int;
public function ApplyPoints (curPoints : int){
Debug.Log(curPoints);
}
I would not suggest using GameObject.Find either, you can get gcoregui getcomponent from score script in start function, and then use it whenever you want. I'm not an JS (unityscript) guy, but I hope i understanded what you wanted.
Also, you should add a tag check before actually getting the component, because for now, it will try to get that component from everything it touches, but as those gamobject don't have that "Score" script, you will get an null reference error.
Answer by xandermacleod · Dec 16, 2013 at 01:53 AM
almost certainly your problem will be with collision detection and not the scripts themselves. In your OnCollisionEnter function try a basic Debug.Log() and test it to see if this is true.
Your answer
Follow this Question
Related Questions
How to save score for survival time? 1 Answer
Scoring help ! 1 Answer
I want my score to reset back to 0 but keep my highscore saved 3 Answers
Unity Game Score Script 1 Answer