- Home /
Is there another way of counting Scores (points) instead of Colliding (destroying) ??
Hello Everyone! I am pretty new to unity! I am developing a serious game in which the person needs to touch with a sphere all the cubes (that will immediately change color) in the environment. I was wondering if there is a way to count points since I am not destroying objects in my game but I am just changing the color of an object for that i want give a point.
Answer by NMJ_GD · Jun 03, 2020 at 08:08 AM
Firstly, if you are new to unity start watching videos or reading studying material. The question you ask is quit basic, use Triggers.
if you are already moving your object around I would assume you are using Rigidbody if you give your other gameobjects collider and check trigger box. They would still interact than the "player" collides with them but now the player would simply past through them and using OnTriggerEnter (OnTriggerExit, OnTriggerStay, which ever you want) methods the collision would be detected and calling your logic through this method it would happen without destroying the objects. Or you can you OnCollisionEnter and then the collision would happen the logic you wrote in the method would happen but know you cant pass through the object.
Overall, Firstly it would be best if you would read documentation, in this case you need to read this part. But start to google and youtube search your answers and after not finding them ask here.
Thank you for your suggestion. Actually I already managed to write something very simple that would count the score. The real problem is that every time the sphere touches a cube, although this has already been touched and has already changed color, the score increases. Ins$$anonymous$$d, the aim would be to give a point only when it is touched the first time (ignoring all the other collisions).
This is the code that I write to count the score: using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI;
public class ScoreScript : $$anonymous$$onoBehaviour {
public static int scoreValue = 0;
Text score;
// Start is called before the first frame update
void Start()
{
score = GetComponent<Text>();
}
// Update is called once per frame
void Update()
{
score.text = "Score: " + scoreValue;
}
}
This code was also recalled in the script aimed at changing the color of the cubes:
void OnCollisionEnter(Collision collision) { if(collision.gameObject.tag == "player") { ScoreScript.scoreValue += 1; rend.shared$$anonymous$$aterial = material[1]; } }
Of course it would change score, your script not only changes score but it even changes the material (maybe you cant see it but for the code it is happening)(it is like giving int i = 1; i = 1;) $$anonymous$$aybe The i is 1 in the end but you gave it 1 several times in reality. In your code You've problem in
void OnCollisionEnter(Collision collision)
{
if(collision.gameObject.tag == "player")
{
ScoreScript.scoreValue += 1;
rend.shared$$anonymous$$aterial = material[1];
}
}
if(collision.gameObject.tag == "player") ----> this part of the code is only one which only checks if the gameobject collision is correct one but it doesn't check if the collision already happened. what you can do is here are many different things, but for know most easy one for you would be if you would give this script one more boolean which checks of it was already collided or not Like this:
public class ColliderCkecker : $$anonymous$$onoBehaviour
{
private bool isChanged = false;
private void Awake()
{
isChanged = false;
}
OnCollisionEnter(Collision col)
{
if(col.gameObject.tag == "Player" && isChanged == false)
{
//your logic
}
}
}
of course this thing can be done another ways but for now it should be enough.
In addition, Please don't write codes on one line. '{', '}' or ';' they mean that after them comes new line