Search Unity

Different scores with different objects in collider triggers.

Discussion in 'Scripting' started by Silveralby, Feb 16, 2019.

  1. Silveralby

    Silveralby

    Joined:
    Jan 14, 2018
    Posts:
    81
    Hi I found a script on the net to apply a score when an object is hit. Is it possible in the same script to give different scores to different objects hit? Thank you
     
  2. f2pallglory

    f2pallglory

    Joined:
    Jan 11, 2019
    Posts:
    25
    Of course it is possible, just give the object a tag or something then make an if(collision.gameObject.tag == "objectScore1"){
    score ++;
    }
     
  3. Silveralby

    Silveralby

    Joined:
    Jan 14, 2018
    Posts:
    81
    Hi f2pallglory thanks for the reply. I think I made a mistake in transcribing the command because in this way assigns 6 points by striking the object "Anello" while with the object "Anello2" nothing happens. Anello2 has the tick "is trigger".

    void OnTriggerEnter(Collider collision)
    {
    //Check the provided Collider parameter other to see if it is tagged "PickUp", if it is...

    if (collision.gameObject.tag == "Anello"){

    count ++;
    }
    //... then set the other object we just collided with to inactive.

    collision.gameObject.SetActive(false);

    //Add one to the current value of our count variable.
    count = count + 1;

    //Update the currently displayed count by calling the SetCountText function.
    SetCountText();

    gameObject.GetComponent<AudioSource>().Play();


    if (collision.gameObject.tag == "Anello2") {

    count++;
    }

    collision.gameObject.SetActive(false);

    //Add one to the current value of our count variable.
    count = count + 5;

    //Update the currently displayed count by calling the SetCountText function.
    SetCountText();

    }

    collision.gameObject.SetActive(false);

    count = count + 5;

    }
     
  4. Silveralby

    Silveralby

    Joined:
    Jan 14, 2018
    Posts:
    81
    Does anyone know how I can solve?
     
  5. f2pallglory

    f2pallglory

    Joined:
    Jan 11, 2019
    Posts:
    25
    im sorry it is a little hard to understand your code please use code tag whenever you post a code

    and based on your code the count variable will always increased by 6 when the object collide, and there is no difference between colliding with object tagged anello and anello2 since both will just increase the count by 1

    if what you want is when collided with anello it increase score by1 and anello2 increase score by 5 then, you need to delete count = count +1 since its already with count++ inside if, and change count++ in if anello2 to count+=5 then delete the count = count+5

    you are using onTriggerEnter so it should work with anello2 with isTrigger on, and need a little more detail on your project to solve this.[/code]
     
  6. Silveralby

    Silveralby

    Joined:
    Jan 14, 2018
    Posts:
    81
    Hi thank's for reply. So there was a repetition that created problems. I correctly place the script I edited but it makes me error in SetCountText (); It is probable that I am wrong to insert a few brackets.
    According to you this script is mandatory to insert it in the object or better to create a empty object in the scene, call it "ScoreManager" and insert it inside?

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using UnityEngine.UI;
    4. using UnityEngine.SceneManagement;
    5.  
    6. public class ScoreManager : MonoBehaviour
    7. {
    8.  
    9.  
    10.     public Text PuntiText;          //Store a reference to the UI Text component which will display the number of pickups collected.
    11.  
    12.  
    13.     private int count;              //Integer to store the number of pickups collected so far.
    14.  
    15.     // Use this for initialization
    16.     void Start()
    17.     {
    18.  
    19.         //Initialize count to zero.
    20.         count = 0;
    21.  
    22.         //Call our SetCountText function which will update the text with the current value for count.
    23.         SetCountText();
    24.  
    25.  
    26.     }
    27.  
    28.     //OnTriggerEnter is called whenever this object overlaps with a trigger collider.
    29.     void OnTriggerEnter(Collider other)
    30.     {
    31.         //Check the provided Collider parameter other to see if it is tagged "PickUp", if it is...
    32.         if (other.gameObject.CompareTag("Anello"))
    33.  
    34.             //... then set the other object we just collided with to inactive.
    35.             other.gameObject.SetActive(false);
    36.  
    37.         //Add one to the current value of our count variable.
    38.         count++;
    39.  
    40.         //Update the currently displayed count by calling the SetCountText function.
    41.         SetCountText();
    42.  
    43.         GetComponent<AudioSource>().Play();
    44.  
    45.         {
    46.             if (other.gameObject.CompareTag("Anello2"))
    47.  
    48.                 //... then set the other object we just collided with to inactive.
    49.                 other.gameObject.SetActive(false);
    50.  
    51.             //Add one to the current value of our count variable.
    52.          count+=5;
    53.  
    54.             //Update the currently displayed count by calling the SetCountText function.
    55.          SetCountText();
    56.  
    57.           GetComponent<AudioSource>().Play();
    58.  
    59.         }
    60.  
    61.             //This function updates the text displaying the number of objects we've collected and displays our victory        message if we've collected all of them.
    62.             void SetCountText()
    63.             {
    64.                 //Set the text property of our our countText object to "Count: " followed by the number stored in our count variable.
    65.                 PuntiText.text = "Punti: " + count.ToString();
    66.  
    67.                 //Check if we've collected all 12 pickups. If we have...
    68.                 if (count >= 20)
    69.  
    70.                     SceneManager.LoadScene("HaiVinto");
    71.  
    72.  
    73.             }
    74.         }
    75.  
    76.     }
     
    Last edited: Feb 22, 2019
  7. Silveralby

    Silveralby

    Joined:
    Jan 14, 2018
    Posts:
    81
    Anyone willing to tell me where I was wrong? thank you.
     
  8. Silveralby

    Silveralby

    Joined:
    Jan 14, 2018
    Posts:
    81
    I try again you can make a single script and enter it as a game manager to get different scores by hitting different objects or you need to apply the same script to each object by changing the score?