Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Feedback I can't find the problem :(

Discussion in 'Scripting' started by Ahmedxp30, Jan 9, 2020.

  1. Ahmedxp30

    Ahmedxp30

    Joined:
    Jan 4, 2020
    Posts:
    2
    - I have 4 objects (Buttons with the same animations and scripts)
    - 3D Project

    - What should happen?
    = When I click on the button(any button) the animation of this button only going to play.

    - But what is actually happen?
    = When I click on the button(any button) all the buttons going to play the same animation at the same time!

    This is my script:
    Code (CSharp):
    1. using System.Collections;
    2.  
    3. using System.Collections.Generic;
    4.  
    5. using UnityEngine;
    6.  
    7.  
    8.  
    9. public class ButtonScript : MonoBehaviour {
    10.  
    11.    
    12.  
    13.     public GameObject lamp1;
    14.  
    15.     public GameObject lamp2;
    16.  
    17.     public GameObject lightObject;
    18.  
    19.     public bool checker = true;
    20.  
    21.     public float range = 3f;
    22.  
    23.    
    24.  
    25.     Animator anim;
    26.  
    27.    
    28.  
    29.    
    30.  
    31.     // Use this for initialization
    32.  
    33.     void Start ()
    34.  
    35.     {
    36.  
    37.         anim = GetComponent<Animator>();
    38.  
    39.     }
    40.  
    41.    
    42.  
    43.     // Update is called once per frame
    44.  
    45.     void Update ()
    46.  
    47.     {
    48.  
    49.         if (Input.GetMouseButtonDown(0))
    50.  
    51.         {
    52.  
    53.             Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
    54.  
    55.             RaycastHit hit;
    56.  
    57.            
    58.  
    59.             if (Physics.Raycast(ray, out hit, range))
    60.  
    61.             {
    62.  
    63.                 if (checker == true)
    64.  
    65.                 {
    66.  
    67.                     anim.SetBool("LightStatue",false);
    68.  
    69.                     checker = false;
    70.  
    71.                 }
    72.  
    73.                 else if (checker == false)
    74.  
    75.                 {
    76.  
    77.                     anim.SetBool("LightStatue",true);
    78.  
    79.                     checker = true;
    80.  
    81.                 }
    82.  
    83.             }
    84.  
    85.         }
    86.  
    87.     }
    88.  
    89. }
    90.  
    91.  
     
  2. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    So when you press the mouse button, all 4 instances in this script will fire their raycasts on line 59. If the raycasts hit anything, and all 4 will have the same result since they are all being fed the same info, then the code for all 4 instances gets inside that "if" statement. Not sure what you're doing with "checker" but if it is the same value on all 4 buttons then all 4 will turn on the animation at the same time.

    I'm pretty sure what you need to do is check what the raycast actually hit to make sure it hit this button instance instead of just hitting anything. You'd likely use that "hit" variable which you're using in the raycast itself.

    https://docs.unity3d.com/ScriptReference/RaycastHit.html
     
  3. Ahmedxp30

    Ahmedxp30

    Joined:
    Jan 4, 2020
    Posts:
    2
    Thanks for the replay but I fixed it :)