Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by HunterKrech · Feb 08, 2013 at 04:07 AM · c#variablelocalunassigned

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); } }

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by ThePunisher · Feb 08, 2013 at 05:21 AM

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

10 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Accessing a variable from js with c# script 1 Answer

storing the value in a temporary variable problem 1 Answer

Converting this JS code to C# 1 Answer

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges