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. Dismiss Notice

Selection Management c#

Discussion in 'Scripting' started by Megalithic, Sep 4, 2014.

  1. Megalithic

    Megalithic

    Joined:
    Apr 21, 2014
    Posts:
    61
    Here I have been trying to create a selection management so that at any given time there will be only one gameobject selected or highlighted. I have done my utmost and here I have put the script. The thing is, i cant seem to alternate selections and I often get a "NullReferene". The script is partly working and partly not. Can you help me out? I need to know how to get thing done.

    Code (CSharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4.  
    5. public class MouseControl1 : MonoBehaviour {
    6.     //This scripts handles the actions performed by the mouse
    7.  
    8.     public GameObject currentlyselected;
    9.     public bool selected = false;
    10.     public RaycastHit hit;
    11.  
    12.     public void Start ()
    13.     {
    14.  
    15.     }
    16.  
    17.     public void Update()
    18.     {
    19.         MouseController();
    20.     }
    21.  
    22.     //This funcition controls the actions performed by the mouse
    23.     public void MouseController()
    24.     {
    25.         //if the left mouse button is down
    26.         if(Input.GetMouseButtonDown(0))
    27.         {
    28.             //Player pressed the mousebutton
    29.             Debug.Log ("Player pressed the mousebutton.");
    30.             //cast a ray into the scene to detect objects
    31.             Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
    32.             if(Physics.Raycast(ray, out hit, Mathf.Infinity))
    33.             {
    34.                 //print the name of the object the ray collided
    35.                 Debug.Log(hit.transform.gameObject.name);
    36.             }
    37.  
    38.             //if the ray hits a player
    39.             if(hit.transform.gameObject.tag == "Player")
    40.             {
    41.                 currentlyselected = hit.transform.gameObject;
    42.                 Select();
    43.                 Debug.Log(1);
    44.             } else {
    45.                 if(selected == true)
    46.                 {
    47.                     Deselect();
    48.                     currentlyselected = hit.transform.gameObject;
    49.                     Select();
    50.                     Debug.Log(2);
    51.                 }
    52.             }
    53.  
    54.             //if the ray doesn't hit the player and the object is selected
    55.             if(hit.transform.gameObject.tag != "Player" && selected == true)
    56.             {
    57.                 Deselect();
    58.                 Debug.Log(3);
    59.             } else {
    60.                 if(selected != true)
    61.                 {
    62.                     Debug.Log("There was nothing there.");
    63.                     Debug.Log(4);
    64.                 }
    65.             }
    66.         }
    67.     }
    68.  
    69.     public void Select()
    70.     {
    71.         currentlyselected.transform.FindChild("Blob Light Projector").gameObject.SetActive(true);
    72.         selected = true;
    73.     }
    74.  
    75.     public void Deselect()
    76.     {
    77.         currentlyselected.transform.FindChild("Blob Light Projector").gameObject.SetActive(false);
    78.         currentlyselected = null;
    79.         selected = false;
    80.     }
    81.  
    82. }
    83.  
     
  2. Patico

    Patico

    Joined:
    May 21, 2013
    Posts:
    886
    What is line number where you gon an error?

    I see couple of danger strokes:
    Code (csharp):
    1. currentlyselected.transform.FindChild("Blob Light Projector").gameObject.SetActive(true)
    It will give you an NullRef exception in case when "Blob Light Projector" object is not exist.
    This is more safety code style:
    Code (csharp):
    1. var go = currentlyselected.transform.FindChild("Blob Light Projector");
    2. if(go!= null)
    3.    go.gameObject.SetActive(true);
    4. else
    5.    Debug.Log("'Blob Light Projector' was not found among children of '"+currentlyselected.Name+"'");
     
    Last edited: Sep 4, 2014
    Megalithic likes this.
  3. THoeppner

    THoeppner

    Joined:
    Oct 10, 2012
    Posts:
    205
    I'm thinking you have to put the lines 38 to 65 also into the if clause which starts at line 32. It should look like this:

    Code (CSharp):
    1.            
    2.             if(Physics.Raycast(ray, out hit, Mathf.Infinity))
    3.             {
    4.                 //print the name of the object the ray collided
    5.                 Debug.Log(hit.transform.gameObject.name);
    6.                 //if the ray hits a player
    7.                 if(hit.transform.gameObject.tag == "Player")
    8.                 {
    9.                     currentlyselected = hit.transform.gameObject;
    10.                     Select();
    11.                     Debug.Log(1);
    12.                 } else {
    13.                     if(selected == true)
    14.                     {
    15.                         Deselect();
    16.                         currentlyselected = hit.transform.gameObject;
    17.                         Select();
    18.                         Debug.Log(2);
    19.                     }
    20.                 }
    21.    
    22.                 //if the ray doesn't hit the player and the object is selected
    23.                 if(hit.transform.gameObject.tag != "Player" && selected == true)
    24.                 {
    25.                     Deselect();
    26.                     Debug.Log(3);
    27.                 } else {
    28.                     if(selected != true)
    29.                     {
    30.                         Debug.Log("There was nothing there.");
    31.                         Debug.Log(4);
    32.                     }
    33.                 }
    34.             }            
    35.  
    Otherwise you check in your if statements for the tags of the gameobjects, but the ray hasn't hit something.
     
    Megalithic likes this.
  4. Megalithic

    Megalithic

    Joined:
    Apr 21, 2014
    Posts:
    61
    Well that does make sense but I think I will try what patico recommended. After posting this question in the forum even his solution occurred to me.
     
  5. Megalithic

    Megalithic

    Joined:
    Apr 21, 2014
    Posts:
    61
    Cheers mate! I will give this a try...