Use of unassigned local variable `selection' Destroying instance

This is blowing my mind! Just made the switch from js to c# and seems i cant destroy an instantiated variable from outside the if statement. So pretty much the code just selects an object and I want it to deselect when I click on the map. Anyway i keep getting the error"Use of unassigned local variable selection". Thanks in advance for any help.

`public class SelectObject : MonoBehaviour {
public GameObject MouseDownTex;
void Update(){

	Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
	RaycastHit hit; 
	object selection;
		if(Input.GetButtonDown("Fire1")){ //Fire1 = LeftMouse
		
			if(Physics.Raycast(ray,out hit,100)){ //Shoots raycast from mousePos for 100 units down across y 
			
				if(hit.collider.tag == "Squad"){ //If the object hit by the raycasts tag is Squad
				 selection = Instantiate(MouseDownTex,new Vector3(hit.collider.transform.position.x,hit.collider.transform.position.y,hit.collider.transform.position.z),Quaternion.identity);
				
				}
			if(hit.collider.tag == "Map"){
					Destroy(selection);
			}
			
	}`

Well it seems the problem is that you are able to “Fire1”, or click I suppose, on the Map without ever having hit a Squad, which is what would set your selection.

Don’t worry, it’s not c# here, it’s you :). You are ray casting and checking if it hit two different things on the same frame, meaning you can never hit both. You either hit Squad, or you hit Map. So the only two scenarios that can happen are as follows.

  1. You left mouse click and you hit squad, that sets your selection, but since your selection variable is local (like the errors says), it will not exist after the method is exited. Next, when you click on the map, all of a sudden (but not really :p) selection is null.

  2. You left mouse click on the map, but selection was never set (again because it’s local to the method you defined it in) so Unity complains are you trying to destroy a null object.

What you need to do is this.

using UnityEngine;
using System.Collections;

public class HunterKrech : MonoBehaviour
{
    public GameObject MouseDownText;
    private Object selection;
	
	// Update is called once per frame
    void Update()
    {
        Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
        RaycastHit hit;

        if (Input.GetButtonDown("Fire1"))
        { //Fire1 = LeftMouse

            if (Physics.Raycast(ray, out hit, 100))
            { //Shoots raycast from mousePos for 100 units down across y

                if (hit.collider.tag == "Squad")
                { //If the object hit by the raycasts tag is Squad
                    selection = Instantiate(MouseDownText, new Vector3(hit.collider.transform.position.x, hit.collider.transform.position.y, hit.collider.transform.position.z), Quaternion.identity);
                }

                if (hit.collider.tag == "Map" && selection != null)
                {
                    Destroy(selection);
                }
            }
        }
    }
}

Notice how I made selection a member variable of the class? Now it will persist through the method calls when you set it. I also added a null check to your Map tag condition. This will insure that you only destroy the selection if there is one.

Hope it helps, let me know if you run into other problems.