- Home /
Need help with adding score from 2 different script C#
Hi guys! so my first script is when the player pick up an gameobject A, player get 3 points and if the player pick up gameobject B, the player get 5 points.
So as the player keep picking up these objects, the score will keep adding up and this is working fine.
However, I have another script that have gameobject C ( Rocks ) which player have to avoid. if gameobject C collides with a gameobject D (lava), the player get 1 points. (originally, I want the player to get 1 point for avoiding rock, but I find that really difficult to do so i decided to destroy rock and get point instead)
The only problem I am having is that the score does not add both script together as one instead it adds as 2 different score. so if my player pick up 2 gameobject A, the player get 6 points. and if the gameobject C collides with gameobject D, player get 1 points. so the point should have add up to 7 but it didn't, the score went back to 1.
here's the script for the pick up part
public Text scoreText;
public int lettuceValue;
public int wormValue;
private int point;
// Use this for initialization
void Start ()
{
point = 0;
SetCountText ();
}
// Update is called once per frame
void Update ()
{
}
void OnTriggerEnter2D(Collider2D other)
{
if (other.gameObject.CompareTag ("PickUp")) {
Destroy (other.gameObject);
point += wormValue;
SetCountText ();
}
else if (other.gameObject.CompareTag ("PickUp2")) {
Destroy (other.gameObject);
point += lettuceValue;
SetCountText ();
}
}
void SetCountText ()
{
scoreText.text = "score:" + point.ToString();
}
}
and this is the avoid script
public Text scoreText;
private int point;
void Start ()
{
point = 0;
SetCountText ();
}
void Update ()
{
}
// in order for this to work, you must have the collider2D isTrigger AND one of the gameObject MUST have a rigidbody2D
void OnTriggerEnter2D(Collider2D other)
{
if (other.gameObject.CompareTag ("Load"))
{
Destroy (other.gameObject);
point = point + 1;
SetCountText ();
}
}
void SetCountText ()
{
scoreText.text = "score: " + point.ToString ();
}
}
So how do I get those 2 script scores add everything up as one?
Answer by Maurdekye · May 29, 2016 at 02:28 AM
Create a third gameobject and attach this script to it;
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class PointsManager : MonoBehaviour {
public Text scoreText;
private int Points;
public void Start()
{
Points = 0;
scoreText.text = "score:" + Points;
}
public void AddPoints(int amount)
{
Points = Points + amount;
scoreText.text = "score:" + Points;
}
}
Then, modify your first script to look like this;
public int lettuceValue;
public int wormValue;
public PointsManager manager;
void OnTriggerEnter2D(Collider2D other)
{
if (other.gameObject.CompareTag("PickUp"))
{
Destroy(other.gameObject);
manager.AddPoints(wormValue);
}
else if (other.gameObject.CompareTag("PickUp2"))
{
Destroy(other.gameObject);
manager.AddPoints(lettuceValue);
}
}
And your second script to look like this;
public PointsManager manager;
// in order for this to work, you must have the collider2D isTrigger AND one of the gameObject MUST have a rigidbody2D
void OnTriggerEnter2D(Collider2D other)
{
if (other.gameObject.CompareTag("Load"))
{
Destroy(other.gameObject);
manager.AddPoints(1);
}
}
Then just pass the first empty gameobject as an argument to both of your other scripts. The point total should keep a rolling count from both of them.
Phew it worked xD!!! Thank you so much for taking the time to answer this! $$anonymous$$uch appreciated!
Answer by Gaming-Dudester · May 29, 2016 at 02:25 AM
Sorry if I misunderstand BUT wouldn't you do this? put this in avoid script
public PickUpScript pickupscript;
and put this in pick up script
public AvoidScript avoidscript;
and say
if(//player picks up thingy)
{
avoidscript.scoreint = avoidscript.scoreint + 1;
}
Answer by naphid · May 29, 2016 at 03:07 AM
Make your points public static instead of private. When they are private it means there can be multiple instances of them.
Answer by Maurdekye · May 29, 2016 at 03:07 AM
Create a third gameobject and attach this script to it;
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class PointsManager : MonoBehaviour {
public Text scoreText;
private int Points;
public void Start()
{
Points = 0;
scoreText.text = "score:" + Points;
}
public void AddPoints(int amount)
{
Points = Points + amount;
scoreText.text = "score:" + Points;
}
}
Then, modify your first script to look like this;
public int lettuceValue;
public int wormValue;
public PointsManager manager;
void OnTriggerEnter2D(Collider2D other)
{
if (other.gameObject.CompareTag("PickUp"))
{
Destroy(other.gameObject);
manager.AddPoints(wormValue);
}
else if (other.gameObject.CompareTag("PickUp2"))
{
Destroy(other.gameObject);
manager.AddPoints(lettuceValue);
}
}
And your second script to look like this;
public PointsManager manager;
// in order for this to work, you must have the collider2D isTrigger AND one of the gameObject MUST have a rigidbody2D
void OnTriggerEnter2D(Collider2D other)
{
if (other.gameObject.CompareTag("Load"))
{
Destroy(other.gameObject);
manager.AddPoints(1);
}
}
Then just pass the first empty gameobject as an argument to both of your scripts.