Search Unity

Cannot select/Deselect cloned gameobject with mouse click

Discussion in 'Scripting' started by pus2meong, Apr 16, 2013.

  1. pus2meong

    pus2meong

    Joined:
    May 3, 2012
    Posts:
    83
    Hi, I got an unsolved problem here.

    Look this screenshot, there are two characters on the map.

    $greenring.jpg

    I'm writing a mouse click script (left click). When I click on the character, she will be selected and a green ring appear under her foot. When I click her again, she will be unselected and the green ring is dissapear.

    The problem is, when I create the clone of the character, the script is not working. BUT, when I remove the deselect command, it works!
    I can select those two character (but cannot deselect because I remove the script)

    I really don't have any idea....

    The click to select script is using raycast. When the raycast hit the character, it will read the tag from the character's collider first. If the character's tag is "Character" it mean the game object is selectable and it will activate the green ring (using projector). The green ring is a child of the character object and in deactive status.

    Here is the script for activate the green ring:

    Code (csharp):
    1.  
    2. function ActivateGreenRing(){
    3.     switch (Selected){ //Is it selected or not?
    4.     case false:
    5.       GreenRing.gameObject.active=true;
    6.       Selected=true;
    7.       Debug.Log("True"); //print true for confirmation
    8.     break;
    9.     case true:
    10.       GreenRing.gameObject.active=false;
    11.       Selected=false;
    12.       Debug.Log("False"); //print false for confirmation
    13.     break;
    14.     }
    15. }
    16.  
    If only one character on the map, this script will work. Select and deselect the character will do just fine. But when the same character (clone) is on the map, it will not working.
    However the debug log is printing both status, True and False when I click the character for the first time.

    And if I do this:
    Code (csharp):
    1.  
    2. function ActivateGreenRing(){
    3.     switch (Selected){
    4.     case false:
    5.       GreenRing.gameObject.active=true;
    6.       Selected=true;
    7.       Debug.Log("True");
    8.     break;
    9.     }
    10. }
    11.  
    The script is working. I click the first character, she was selected. Then click her clone, she also got selected.

    So, any idea where do I go wrong?
     
  2. wolfhunter777

    wolfhunter777

    Joined:
    Nov 3, 2011
    Posts:
    534
    Why are you using a Switch statement for a boolean variable? Isn't an if-statement way easier and better? Can you show your input codes? How are you triggering your selected variable?
     
  3. pus2meong

    pus2meong

    Joined:
    May 3, 2012
    Posts:
    83
    Because I already did that before and still failed:
    Here is what I do previously.
    Code (csharp):
    1. function ActivateGreenRing(){
    2. If (!Selected){ //Is it selected or not
    3.       GreenRing.gameObject.active=true;
    4.       Selected=true;
    5.       Debug.Log("True"); //print true for confirmation
    6.  
    7.     } else {
    8.       GreenRing.gameObject.active=false;
    9.       Selected=false;
    10.       Debug.Log("False"); //print false for confirmation
    11.    
    12.     }
    13.  
    14. }
    15.  
    I also already use the GreenRing.gameObject.active as the condition, still resulting the same thing
    Code (csharp):
    1. If (!GreenRing.gameObject.active){ //Is it active or not
    2.       GreenRing.gameObject.active=true;
    3.       Selected=true;
    4.       Debug.Log("True"); //print true for confirmation
    5.  
    6.     } else {
    7.       GreenRing.gameObject.active=false;
    8.       Selected=false;
    9.       Debug.Log("False"); //print false for confirmation
    10.    
    11.     }
    12.  
    13. }
     
  4. wolfhunter777

    wolfhunter777

    Joined:
    Nov 3, 2011
    Posts:
    534
    Sorry, I meant are you checking the original prefab by any chance? Also, how are you detecting when you set your Selected variable?
     
  5. pus2meong

    pus2meong

    Joined:
    May 3, 2012
    Posts:
    83
    Original prefab? You mean the prefab in the Project window?
    I don't think so, because I just right click the gameobject from the Hierarchy window and choose duplicate to make the clone. So both object will got the same properties (including the script), but not the position.

    Here is how I detecting the game object with raycast:

    Code (csharp):
    1.  
    2. //PROBLEM
    3. function MouseClickLeft(){
    4.     var BeamedObjectHit:Transform;
    5.     if (Input.GetMouseButtonDown(0)){
    6.         //GameStatusV2.SelectAllKnight=false;
    7.         BeamedObjectHit=BeamingV2(1);
    8.         if (BeamedObjectHit.tag == "Character"){
    9.             BeamedObjectHit.SendMessage("ActivateGreenRing");
    10.         }
    11.     }
    12. }
    13.  
    14.  
    15. function BeamingV2(Param:int){
    16.     var BeamDestV2:Ray = Camera.main.ScreenPointToRay(Input.mousePosition);
    17.     var BeamedObject:RaycastHit;
    18.     if (Physics.Raycast(BeamDestV2, BeamedObject)){
    19.         if (Param == 1){
    20.             return BeamedObject.transform;
    21.         }
    22.         if (Param == 2){
    23.             return BeamedObject.point;//Return Vector 3 coordinate
    24.         }
    25.     }
    26.    
    27. }