Search Unity

[Resolved ]Score different form type

Discussion in '2D' started by simone9725, May 16, 2015.

  1. simone9725

    simone9725

    Joined:
    Jul 19, 2014
    Posts:
    234
    Could I ask a rapid question?
    in the hat trick game when I catch the ball I can't add a diffent score from 1 and I 've a problem as in the picture
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class Ball1 : MonoBehaviour {
    5.     public GUIText scoreText;
    6.     private int score;
    7.  
    8.     // Use this for initialization
    9.     void Start () {
    10.  
    11.     }
    12.     void OnCollisionEnter2D (Collision2D collision) {
    13.         if (collision.gameObject.tag == "Hat") {
    14.         score += 2;
    15.             UpdateScore ();
    16. }
    17.     }
    18.     void UpdateScore () {
    19.             scoreText.text = "SCORE:\n" + score;
    20.     }
    21. }
    Could someone help pls ?
    Thanks
     

    Attached Files:

    Last edited: May 17, 2015
  2. Gardes

    Gardes

    Joined:
    Apr 7, 2015
    Posts:
    46
    Try this:
    Code (CSharp):
    1.     void OnCollisionEnter2D (Collision2D collision) {
    2.         if (collision.gameObject.CompareTag ("Hat")) {
    3.             score += 2;
    4.             UpdateScore ();
    5.         }
    6.     }
     
  3. simone9725

    simone9725

    Joined:
    Jul 19, 2014
    Posts:
    234
    I ve the same issue so I changed the score script but nothing
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class HT_Score : MonoBehaviour {
    5.  
    6.     public GUIText scoreText;
    7.     public int ballValue;
    8.  
    9.     private int score;
    10.  
    11.     void Start () {
    12.         score = 0;
    13.         UpdateScore ();
    14.     }
    15.  
    16.     void OnTriggerEnter2D (Collider2D other) {
    17.         score += ballValue;
    18.         UpdateScore ();
    19.     }
    20.    
    21.     void OnCollisionEnter2D (Collision2D collision) {
    22.         if (collision.gameObject.tag == "Bomb") {
    23.             score -= ballValue * 2;
    24.             UpdateScore ();
    25.             if (collision.gameObject.tag == "BeachBall") {
    26.                 score += ballValue * 2;
    27.                 UpdateScore ();
    28.         }
    29.     }
    30.     }
    31.     void UpdateScore () {
    32.         scoreText.text = "SCORE:\n" + score;
    33.     }
    34. }
    35.  
     
  4. Gardes

    Gardes

    Joined:
    Apr 7, 2015
    Posts:
    46
    The only logic error I see, is your if in if statement. Your second if statment can only happen, if the first is true and I guess this will never happen in this game

    Code (CSharp):
    1.     void OnCollisionEnter2D (Collision2D collision) {
    2.         if (collision.gameObject.tag == "Bomb") {
    3.             score -= ballValue * 2;
    4.             UpdateScore ();
    5.         }
    6.  
    7.         if (collision.gameObject.tag == "BeachBall") {
    8.             score += ballValue * 2;
    9.             UpdateScore ();
    10.         }
    11.     }
     
  5. simone9725

    simone9725

    Joined:
    Jul 19, 2014
    Posts:
    234
    Thanks for your answer
    So what a I have to do ?
    My old code is incorrect, the code posted above is impossible so which kind of script should I use?
     
  6. Adam-Buckner

    Adam-Buckner

    Joined:
    Jun 27, 2007
    Posts:
    5,664
    The only difference between:
    Code (CSharp):
    1. if (collision.gameObject.CompareTag ("Hat")) {}
    and
    Code (CSharp):
    1. if (collision.gameObject.tag =="Hat") {}
    is that "CompareTag" is slightly more optimised and therefor a little more efficient.
     
  7. Adam-Buckner

    Adam-Buckner

    Joined:
    Jun 27, 2007
    Posts:
    5,664
    The point that @Gardes is trying to make is that your if statements were nested. You were using:
    Code (CSharp):
    1.    void OnCollisionEnter2D (Collision2D collision) {
    2.         if (collision.gameObject.tag == "Bomb") {
    3.             score -= ballValue * 2;
    4.             UpdateScore ();
    5.             if (collision.gameObject.tag == "BeachBall") {
    6.                 score += ballValue * 2;
    7.                 UpdateScore ();
    8.         }
    9.     }
    10.     }
    Now, a point to following convention and being tidy: Because your braces are all over the place and items are not indented correctly, it's hard to see your error. If you were to write this correctly it would look like this:
    Code (CSharp):
    1.    void OnCollisionEnter2D (Collision2D collision) {
    2.        if (collision.gameObject.tag == "Bomb") {
    3.             score -= ballValue * 2;
    4.             UpdateScore ();
    5.             if (collision.gameObject.tag == "BeachBall") {
    6.                   score += ballValue * 2;
    7.                   UpdateScore ();
    8.             }
    9.         }
    10.     }
    and you would be able to see that the second if is nested inside the first if. When isolated, this example is not so bad, but when looking at the entire script with inconsistent indenting on the code and braces, it makes it hard, at a glance, to know when a mistake has been made.

    The only way the if tag is beachball block would ever get called would be if the tag on the GameObject were both bomb and beachball, which is not possible. A GameObject can only have one tag.

    This code from @Gardes shows how these two different if statements can be written so they work:

    Code (CSharp):
    1.     void OnCollisionEnter2D (Collision2D collision) {
    2.           if (collision.gameObject.tag == "Bomb") {
    3.               score -= ballValue * 2;
    4.               UpdateScore ();
    5.           }
    6.  
    7.           if (collision.gameObject.tag == "BeachBall") {
    8.               score += ballValue * 2;
    9.               UpdateScore ();
    10.           }
    11.     }
    What game objects are you attaching this script to? I see in one attempt, you are looking for the hat, and in another you are looking for the dropping objects. The game is set up so that this only works with one of these two ways.
     
    Gardes likes this.
  8. Adam-Buckner

    Adam-Buckner

    Joined:
    Jun 27, 2007
    Posts:
    5,664
    When it comes to the image, are you saying that your objects are passing through the hat? Can you post your full code for both the hat and the balls?
     
  9. simone9725

    simone9725

    Joined:
    Jul 19, 2014
    Posts:
    234
    The code are the same of hat tricks tutorial It is attached To score The second version, The first was attached to Beach ball
     
    Last edited: May 18, 2015
  10. Adam-Buckner

    Adam-Buckner

    Joined:
    Jun 27, 2007
    Posts:
    5,664
     
  11. simone9725

    simone9725

    Joined:
    Jul 19, 2014
    Posts:
    234
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class HT_HatController : MonoBehaviour {
    5.  
    6.     public Camera cam;
    7.  
    8.     private float maxWidth;
    9.     private bool canControl;
    10.  
    11.     // Use this for initialization
    12.     void Start () {
    13.         if (cam == null) {
    14.             cam = Camera.main;
    15.         }
    16.         Vector3 upperCorner = new Vector3 (Screen.width, Screen.height, 0.0f);
    17.         Vector3 targetWidth = cam.ScreenToWorldPoint (upperCorner);
    18.         float hatWidth = renderer.bounds.extents.x;
    19.         maxWidth = targetWidth.x - hatWidth;
    20.         canControl = false;
    21.     }
    22.    
    23.     // Update is called once per physics timestep
    24.     void FixedUpdate () {
    25.         if (canControl) {
    26.             Vector3 rawPosition = cam.ScreenToWorldPoint (Input.mousePosition);
    27.             Vector3 targetPosition = new Vector3 (rawPosition.x, 0.0f, 0.0f);
    28.             float targetWidth = Mathf.Clamp (targetPosition.x, -maxWidth, maxWidth);
    29.             targetPosition = new Vector3 (targetWidth, targetPosition.y, targetPosition.z);
    30.             rigidbody2D.MovePosition (targetPosition);
    31.         }
    32.     }
    33.  
    34.     public void ToggleControl (bool toggle) {
    35.         canControl = toggle;
    36.     }
    37. }
    38.  
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class HT_Score : MonoBehaviour {
    5.  
    6.     public GUIText scoreText;
    7.     public int ballValue;
    8.  
    9.     private int score;
    10.  
    11.     void Start () {
    12.         score = 0;
    13.         UpdateScore ();
    14.     }
    15.  
    16.     void OnTriggerEnter2D (Collider2D other) {
    17.         score += ballValue;
    18.         UpdateScore ();
    19.     }
    20.    
    21.     void OnCollisionEnter2D (Collision2D collision) {
    22.         if (collision.gameObject.tag == "Bomb") {
    23.             score -= ballValue * 2;
    24.  
    25.             }
    26.     }
    27.  
    28.     void UpdateScore () {
    29.         scoreText.text = "SCORE:\n" + score;
    30.     }
    31. }
    32.  
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class Ball1 : MonoBehaviour {
    5.     public GUIText scoreText;
    6.     private int score;
    7.  
    8.     // Use this for initialization
    9.     void Start () {
    10.    
    11.     }
    12.     void OnCollisionEnter2D (Collision2D collision) {
    13.         if (collision.gameObject.CompareTag ("Hat")) {
    14.             score += 2;
    15.             UpdateScore ();
    16.         }
    17.     }
    18.     void UpdateScore () {
    19.             scoreText.text = "SCORE:\n" + score;
    20.     }
    21. }
     
  12. Adam-Buckner

    Adam-Buckner

    Joined:
    Jun 27, 2007
    Posts:
    5,664
    From me at this code, it seems that you have and Ball1 from scratch.

    This doesn't match the pattern from the two scripts that look like they were copied from the project.

    Looking at the two scripts from the project, it looks like HT_Score is the script that controls the scoring mechanism.

    Shouldn't this be the script that you modify when trying to add new types of falling ball objects?
     
  13. simone9725

    simone9725

    Joined:
    Jul 19, 2014
    Posts:
    234
    No i didn't modify any part of the script, there was the original code of hat game.So about The score i didn't understand what I have To do
     
  14. simone9725

    simone9725

    Joined:
    Jul 19, 2014
    Posts:
    234
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class HT_Score : MonoBehaviour {
    5.  
    6.     public GUIText scoreText;
    7.     public int ballValue;
    8.     public int ballValue2;
    9.     private int score;
    10.  
    11.     void Start () {
    12.         score = 0;
    13.         UpdateScore ();
    14.     }
    15.  
    16.     void OnTriggerEnter2D (Collider2D other) {
    17.         if (gameObject.tag == "BeachBall") {
    18.         score += ballValue;
    19.         UpdateScore ();
    20.         }
    21.  
    22.      if (gameObject.tag == "BowlingBall") {
    23.         score += ballValue2;
    24.         UpdateScore ();
    25.     }
    26.  
    27.     }
    28.  
    29.     void OnCollisionEnter2D (Collision2D collision) {
    30.         if (collision.gameObject.tag == "Bomb") {
    31.             score -= ballValue * 2;
    32.             UpdateScore ();
    33.         }
    34.      
    35.         }
    36.  
    37.     void UpdateScore () {
    38.         scoreText.text = "SCORE:\n" + score;
    39.     }
    40. }
    41.  
    I tried to change the score system but now the score when I hit a ball is always 0,the gameobjects have the correct tag,and ball value1 is set to 1 while,ballvalue2 is set to 2
     
  15. Adam-Buckner

    Adam-Buckner

    Joined:
    Jun 27, 2007
    Posts:
    5,664
    I'm finding your code hard to read. Can you correct your indents and braces, please?
     
  16. simone9725

    simone9725

    Joined:
    Jul 19, 2014
    Posts:
    234
    I don't understand ,the logic is when the hat has a contact with an object with tag Beach Ball or another score add the value of ball value 1 or ball value 2. Where is the mistake?
     
  17. Adam-Buckner

    Adam-Buckner

    Joined:
    Jun 27, 2007
    Posts:
    5,664
     
  18. simone9725

    simone9725

    Joined:
    Jul 19, 2014
    Posts:
    234
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class HT_Score : MonoBehaviour {
    5.  
    6.     public GUIText scoreText;
    7.     public int ballValue;
    8.     public int ballValue2;
    9.     private int score;
    10.  
    11.     void Start () {
    12.         score = 0;
    13.         UpdateScore ();
    14.     }
    15.  
    16.     void OnTriggerEnter2D (Collider2D other) {
    17.         if (gameObject.tag == "BeachBall")
    18.         score += ballValue;
    19.  
    20.         if (gameObject.tag == "BowlingBall")
    21.         score += ballValue2;
    22.         UpdateScore ();
    23.     }
    24.  
    25.  
    26.  
    27.     void OnCollisionEnter2D (Collision2D collision) {
    28.         if (collision.gameObject.tag == "Bomb") {
    29.             score -= ballValue * 2;
    30.             UpdateScore ();
    31.         }
    32.      
    33.         }
    34.  
    35.     void UpdateScore () {
    36.         scoreText.text = "SCORE:\n" + score;
    37.     }
    38. }
    39.  
    It has sense I don't know other solutions
     
  19. Adam-Buckner

    Adam-Buckner

    Joined:
    Jun 27, 2007
    Posts:
    5,664
    Ok. Now you've just removed a bunch of necessary braces.

    Braces or curly brackets surround and identify blocks of code. With out the braces, the c# compiler cannot understand what you are trying to do or how you are containing the scope of your logic.

    Originally you wrote:
    Code (CSharp):
    1.     void OnTriggerEnter2D (Collider2D other) {
    2.         if (gameObject.tag == "BeachBall") {
    3.         score += ballValue;
    4.         UpdateScore ();
    5.         }
    6.  
    7.      if (gameObject.tag == "BowlingBall") {
    8.         score += ballValue2;
    9.         UpdateScore ();
    10.     }
    11.  
    12.     }
    Here the code itself is not inaccurate, but it's hard to read because the indenting is all over the place.

    Then you simple removed braces to get this:
    Code (CSharp):
    1.     void OnTriggerEnter2D (Collider2D other) {
    2.         if (gameObject.tag == "BeachBall")
    3.         score += ballValue;
    4.  
    5.         if (gameObject.tag == "BowlingBall")
    6.         score += ballValue2;
    7.         UpdateScore ();
    8.     }
    This is different logic. Is this the logic you want? It may work, but is it intentional?

    It's late here 1:15 am. I'll try to write more on braces and scope tomorrow.
     
  20. simone9725

    simone9725

    Joined:
    Jul 19, 2014
    Posts:
    234
    It doesn't work the score stay on 0 There isn't any difference
     
  21. Adam-Buckner

    Adam-Buckner

    Joined:
    Jun 27, 2007
    Posts:
    5,664
    I understand that there could be a language issue, so I may not completely understand your comment:
    But, just randomly changing code will probably not help.

    There are two issues here:
    • Code (content and cleanliness)
    • How the code interacts with your project and scene
    Code:

    Code is written in logical blocks.
    These logical blocks are indicated by the { }'s in your code.
    To help understand the logic of the code, the { }'s are used with a series of indents to make the code more readable.
    Compilers (the thing that takes our hand written code and processes it up into our applications) don't care about indents, spaces or returns [at least the ones we use].

    You can write your code like this:
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class HT_Score : MonoBehaviour {
    5.  
    6. public GUIText scoreText;
    7. public int ballValue;
    8. public int ballValue2;
    9. private int score;
    10.  
    11. void Start () {
    12. score = 0;
    13. UpdateScore ();
    14. }
    15.  
    16. void OnTriggerEnter2D (Collider2D other) {
    17. if (gameObject.tag == "BeachBall")
    18. {
    19. score += ballValue;
    20. UpdateScore ();
    21. {
    22.  
    23. if (gameObject.tag == "BowlingBall")
    24. score += ballValue2;
    25. UpdateScore ();
    26. }
    27.  
    28. void OnCollisionEnter2D (Collision2D collision) {
    29. if (collision.gameObject.tag == "Bomb") {
    30. score -= ballValue * 2;
    31. UpdateScore ();
    32. }
    33. }
    34.  
    35. void UpdateScore () {
    36. scoreText.text = "SCORE:\n" + score;
    37. }
    38. }
    39.  
    ... and the compiler won't care.

    It's just very difficult to see exactly what's going on, and hard to spot errors as a human, or non-machine if you're some other species.

    The code could even be written like this:
    Code (CSharp):
    1. using UnityEngine;using System.Collections;public class HT_Score : MonoBehaviour {public GUIText scoreText;public int ballValue;public int ballValue2;private int score;void Start () {score = 0;UpdateScore ();}voidOnTriggerEnter2D (Collider2D other) {if (gameObject.tag == "BeachBall"){score += ballValue;{
    2. if (gameObject.tag == "BowlingBall")score += ballValue2;UpdateScore ();}void OnCollisionEnter2D (Collision2D collision) {if (collision.gameObject.tag == "Bomb") {score -= ballValue * 2;UpdateScore ();}}void UpdateScore () {
    3. scoreText.text = "SCORE:\n" + score;}}
    Which with line wrapping would look like this:
    Impossible to read.

    Almost as impossible to read as this:
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class HT_Score : MonoBehaviour {
    5.  
    6. public GUIText scoreText;
    7. public int ballValue;
    8. public int ballValue2;
    9. private int score;
    10.  
    11. void Start () {
    12.     score = 0;
    13.         UpdateScore ();
    14.             }
    15.  
    16. void OnTriggerEnter2D (Collider2D other) {
    17. if (gameObject.tag == "BeachBall")
    18.     {
    19.             score += ballValue;
    20.     UpdateScore ();
    21. {
    22.  
    23.     if (gameObject.tag == "BowlingBall")
    24. score += ballValue2;
    25.         UpdateScore ();
    26.         }
    27.  
    28.         void OnCollisionEnter2D (Collision2D collision)
    29. {
    30.     if (collision.gameObject.tag == "Bomb") {
    31.     score -= ballValue * 2;
    32.             UpdateScore ();
    33.         }
    34.         }
    35.  
    36.     void UpdateScore () {
    37. scoreText.text = "SCORE:\n" + score;
    38.     }}
    Now, when we write code, it should be even, and the hierarchy of logic should be clearly visible in the { }s and Indenting, like this:
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class HT_Score : MonoBehaviour {
    5.  
    6.     public GUIText scoreText;
    7.     public int ballValue;
    8.     public int ballValue2;
    9.     private int score;
    10.  
    11.     void Start () {
    12.         score = 0;
    13.         UpdateScore ();
    14.     }
    15.  
    16.     void OnTriggerEnter2D (Collider2D other) {
    17.         if (gameObject.tag == "BeachBall"){
    18.             score += ballValue;
    19.             UpdateScore ();
    20.         }
    21.  
    22.         if (gameObject.tag == "BowlingBall"){
    23.             score += ballValue2;
    24.             UpdateScore ();
    25.         }
    26.    }
    27.  
    28.     void OnCollisionEnter2D (Collision2D collision) {
    29.         if (collision.gameObject.tag == "Bomb") {
    30.             score -= ballValue * 2;
    31.             UpdateScore ();
    32.         }
    33.     }
    34.  
    35.     void UpdateScore () {
    36.         scoreText.text = "SCORE:\n" + score;
    37.     }
    38. }
    When we write it this way, we can see everything lines up based on the code blocks and scope of the logic.

    This way we can read the code at a glance.

    Now, as a side note - this is not only true for code scripts, but it is also true for film, tv and all dramatic scripting. These fields are as much if not more insistent on script formatting standards:
    Image from http://www.writersstore.com/how-to-write-a-screenplay-a-guide-to-scriptwriting/

    tldr; Keep your code neat and tidy if you expect someone other than the compiler is going to read it. It will also help you spot errors. Spotting proper logical hierarchy (nesting) and proper braces defining blocks of code are very important, and without proper formatting, it can be missed.
     
  22. Adam-Buckner

    Adam-Buckner

    Joined:
    Jun 27, 2007
    Posts:
    5,664
    Code, Pt2:
    There is a big difference between these two pieces of code (and I've corrected the formatting):

    Originally you wrote:
    Code (CSharp):
    1.     void OnTriggerEnter2D (Collider2D other) {
    2.         if (gameObject.tag == "BeachBall") {
    3.             score += ballValue;
    4.             UpdateScore ();
    5.         }
    6.  
    7.         if (gameObject.tag == "BowlingBall") {
    8.             score += ballValue2;
    9.             UpdateScore ();
    10.         }
    11.     }
    12.  
    Then you updated it to:
    Code (CSharp):
    1.  
    2.     void OnTriggerEnter2D (Collider2D other) {
    3.         if (gameObject.tag == "BeachBall")
    4.             score += ballValue;
    5.  
    6.         if (gameObject.tag == "BowlingBall")
    7.             score += ballValue2;
    8.  
    9.         UpdateScore ();
    10.    }
    One thing to note about { }s... If you omit the braces when writing an if statement, the "if" will only apply to the next line of code. In your cases, if (gameObject.tag == "BeachBall") will only affect score += ballValue; and if (gameObject.tag == "BowlingBall") will only affect score += ballValue2; and because UpdateScore (); is within the root scope of the OnTriggerEnter2D function, it will always be called when your object gets an OnTriggerEnter2D event message.

    What you had written the first time makes more sense, doesn't it? Do you want to call UpdateScore(); if both tests for "BeachBall" and "BowlingBall" fail and test false?
     
  23. simone9725

    simone9725

    Joined:
    Jul 19, 2014
    Posts:
    234
    thanks for your corrections;the problem is that the GUI score isn't update in the last mssage in a bad english I would say that it doesn't matter what changes I have done the score problem still exist
     
  24. Adam-Buckner

    Adam-Buckner

    Joined:
    Jun 27, 2007
    Posts:
    5,664
    How the code interacts with your project and scene

    Now, you can write perfect code and still have your game fail.

    This is because your code needs to be part of, and interact with, the rest of your scene that you have constructed in the editor.

    If you have written perfect code, and you still are getting no score, then you need to look somewhere else for your issue.

    Let's look at the best code that you've written so far (I've corrected the formatting...):
    Code (CSharp):
    1.     void OnTriggerEnter2D (Collider2D other) {
    2.         if (gameObject.tag == "BeachBall") {
    3.             score += ballValue;
    4.             UpdateScore ();
    5.         }
    6.  
    7.         if (gameObject.tag == "BowlingBall") {
    8.             score += ballValue2;
    9.             UpdateScore ();
    10.         }
    11.     }
    12.  
    This says if the other collider's tag is the same as "BeachBall", then set the score to score plus ball value and then update the score display, and then the next, there is another test that says if the other collider's tag is the same as "BowlingBall", then set the score to score plus ball value 2 and then update the score display.

    First thing to check is "Are you getting any errors?", like a Null Reference Error on the variable score, or the variables ballValue or ballValue2? I would assume not, as you've not mentioned any errors in the console.

    If the code is correct, then we can test to see if this two lines are being called.

    Change the code to this:
    Code (CSharp):
    1.     void OnTriggerEnter2D (Collider2D other) {
    2.         if (gameObject.tag == "BeachBall") {
    3.             Debug.Log ("Tag is BeachBall");
    4.             score += ballValue;
    5.             UpdateScore ();
    6.         }
    7.  
    8.         else if (gameObject.tag == "BowlingBall") {
    9.             Debug.Log ("Tag is BowlingBall");
    10.             score += ballValue2;
    11.             UpdateScore ();
    12.         }
    13.        
    14.         else {
    15.             Debug.Log ("OnTriggerEnter2D was called, but No Tags Matched the written Criteria!");
    16.         }
    17.     }
    With this logic, and the if - else if - else means that every time the OnTriggerEnter2D is called, you will get a message.

    Now, I suspect you will either get:
    • NO MESSAGES, which means you need to look into why nothing is participating in the OnTriggerEnter2D event. This would mean: Check your colliders and trigger settings to make sure they are correct.
    • NO TAGS MATCHED..., which would mean you are getting the OnTriggerEnter event message on your GameObject or Prefab, but the conditionals (the if statememnts) are not evaluating to true. In this case you need to check your Tags, and make sure they are correct. The most usual reasons are:
      • No tag was created, so therefore no tag was ever assigned to the GameObject or Prefab.
      • The tag was created but not assigned, which means the GameObject or Prefab is "Untagged" or has the wrong tag.
      • The tag was created incorrectly, which would mean that there is a mismatch between your code and your tag. A common issue here is a typo or mis-spelling. You could have Beachball, beachball, Beach Ball, BeechBal, instead of BeachBall. The same would be true for BowlingBall. One simple solution to this is to copy and paste the value from the script into the tag (or the other way around).
      • The wrong tag is assigned to the GameObject or Prefab, which is easy to do if we're not paying attention. If there are several tags in the project, and one is assigning tags to the GameObject or Prefab and select the incorrect tag, it will fail to behave correctly.
     
  25. simone9725

    simone9725

    Joined:
    Jul 19, 2014
    Posts:
    234
    I ve got the fist error : Debug.Log because in console I haven't any messages and the tags are set corerctly
     
  26. Adam-Buckner

    Adam-Buckner

    Joined:
    Jun 27, 2007
    Posts:
    5,664
    Now that it is formatted correctly and I'm reading it correctly, I can see that you are writing:
    Code (CSharp):
    1. if (gameObject.tag == "TagValue") {
    ... and the short-hand reference gameObject refers to THIS GameObject, or the GameObject that this script is attached to.

    This code is not testing the tag of the GameObject that is colliding with the GameObject that this script is attached to - presumably the Hat.

    The collision is with Collider2D other, where the other is the Collider from the contact being passed in by the event message.

    You need to test the tag on the other Collider:
    Code (CSharp):
    1. if (other.tag == "TagValue") {
    or, slightly more efficiently:
    Code (CSharp):
    1. if (other.CompareTag ("TagValue")) {
    This seems to have been a change in the code from the message on Tuesday at 1:17 PM. Before that, the code was testing the proper item.
     
  27. simone9725

    simone9725

    Joined:
    Jul 19, 2014
    Posts:
    234
    Now it works perfectly !!!!
    There was a problem of other .
    Thank you so much for you time and your help :)
     
  28. simone9725

    simone9725

    Joined:
    Jul 19, 2014
    Posts:
    234
    There was another problem about the sprite in image what Can I do about it?
     
  29. Adam-Buckner

    Adam-Buckner

    Joined:
    Jun 27, 2007
    Posts:
    5,664
    Well, the problem did change over time.

    The original issue was different from the final problem, but I'm glad it's sorted.

    Remember to keep your code neat and tidy, so other people can read it and help you out.

    Who would you prefer to help?

     
  30. Adam-Buckner

    Adam-Buckner

    Joined:
    Jun 27, 2007
    Posts:
    5,664
    Open a new thread with all the details and ping me with @adambuckner