- Home /
Adding coin score to multiplied value
I have a script that causes the score that goes up by 100 every second, and a script that should add 1000 points every coin collided with. Both scripts run, however when the player collides with the coin, 1000 points isn't added to the current score.
Here is my script for score per second:
using UnityEngine;
using System.Collections;
public class ScoreperSec : MonoBehaviour {
public int startingScore = 0;
public int currentScore = 0;
public GUIText scoregui;
private float time;
public static int score;
void Update (){
time+=Time.deltaTime;
//currentScore += (int)time;
print ((int)time);
score = (int)time * 100;
//(int)time * 100 == score;
scoregui.text = "Score: "+ score;
}
}
Here is my script for the coins that should add 1000 points each:
using UnityEngine;
using System.Collections;
public class coins : MonoBehaviour {
// Use this for initialization
void OnTriggerEnter(Collider coll) {
GameObject addScore = GameObject.Find("GUI");
ScoreperSec scoreScript = addScore.GetComponent<ScoreperSec>();
if (coll.gameObject.tag == "Player"){
ScoreperSec.score += 100000;
Destroy(this.gameObject);
}
}
}
Answer by shriya · Jan 23, 2015 at 05:04 AM
Hi,
Got it working.Check below the scripts
using UnityEngine;
using System.Collections;
public class ScoreperSec : MonoBehaviour {
public int startingScore = 0;
public int currentScore = 0;
public GUIText scoregui;
private float time;
public static int score;
public static int localscore;
void Update (){
time+=Time.deltaTime;
print ((int)time);
localscore = (int)time * 100;
score = localscore + coins.coinscore;
Debug.Log ("total score "+score);
// scoregui.text = "Score: "+ score;
}
}
this is the coin one using UnityEngine; using System.Collections;
public class coins : MonoBehaviour {
public static int coinscore;
// Use this for initialization
void OnTriggerEnter(Collider coll) {
Debug.Log ("Trigger enter");
if (coll.gameObject.tag == "Player"){
coinscore += 1158;
Debug.Log ("coins score"+ScoreperSec.score);
Destroy(this.gameObject);
}
}
}
Hope it helps :)
Answer by Redeemer86 · Jan 23, 2015 at 03:58 AM
Coins script ... Line 11
scoreScript.score += 100000;
Red
I made this change and now I get the error: "Assets/Scripts/coins.cs(12,37): error CS0176: Static member `ScoreperSec.score' cannot be accessed with an instance reference, qualify it with a type name ins$$anonymous$$d"
After receiving this error, I deleted the term static in the score script in this line: " public static int score;" and now the coin does not get destroyed, nor does it add points to the score.
Are you sure you have given the tag of player in inspector?
I tested it again and go this error message?:
"NullReferenceException: Object reference not set to an instance of an object coins.OnTriggerEnter (UnityEngine.Collider coll) (at Assets/Scripts/coins.cs:12)"
I just now implemented your code and its working fine. Your error is because of ScoreperSec scoreScript = addScore.GetComponent(); this line. simply comment these lines .you don't even use them