Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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
1
Question by jessesloan1 · Mar 05, 2017 at 01:36 AM · compiler errorwarnings

Unity 5.5.1 - Weird and conflicting variable never used warning.

I have the following in a script:

 private Brick brick;
 
 void Start()
 {
        brick = GameObject.FindObjectOfType<Brick>();
 }

This is only a very small portion of the code in this script.

I keep getting the warning that the variable "brick" is assigned but never used. If i delete the variable, of course, I get an error that says "the name brick does not exist in the current context.

It can't be that I have assigned the variable and simultaneously not assigned it!

Anyone know the answer to this? it's a bit annoying. I'm close to finishing up my game and want to clear out all the warning but this one is stubborn.

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

2 Replies

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

Answer by nug700 · Mar 05, 2017 at 01:48 AM

You get the warning because, based on that code, even though your are assigning brick a value with "brick = GameObject.FindObjectOfType", you are not actually using it. Using it would be calling a method in Brick, getting data from a field/property, equating it to something, etc. This warning is only to notify you that the variable isn't used for anything, and could be removed.

Comment
Add comment · Show 2 · 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
avatar image Bunny83 · Mar 05, 2017 at 02:54 AM 1
Share

Right, note that this warning only shows up for "private" variables. private variables can only be accessed from within the class and therefore the compiler can say for sure that a variable isn't used. If you make it public or protected the warning would disappear.

@jessesloan1: $$anonymous$$eep in $$anonymous$$d that the point of a variable is to store some value / reference for later use. Storing something in a variable and never doing anything with that stored something is pointless. So you have to ask yourself: Why do you actually store that Brick reference in that variable? Either your code is not finished, so the actual use of this variable hasn't been added yet, or it's indeed pointless and the variable as well as the assignment can be removed completely.

avatar image nug700 Bunny83 · Mar 05, 2017 at 03:01 AM 0
Share

Didn't think of the private vs public thing. $$anonymous$$akes complete sense, though, so someone doesn't get a million of these errors when making an API or library.

avatar image
0

Answer by jessesloan1 · Mar 05, 2017 at 04:18 AM

Yeah, this was a big "Derp!" moment for me today. I set that variable and used it and then decided to go back and take a different approach. I guess I forgot all about deleting all of it and then today I did not look any further before I posted this silly question. sigh....

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

65 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Suddenly warnings are thrown as errors at random in Unity 1 Answer

implicit downcast 'Object' to "UnityEngine.Quaternion" 1 Answer

AssetBundle Manager - Update for Unity3D 5.4? 1 Answer

3dplatform tutorial compiler errors out of box -1 Answers

Scripts were perfect but now they are all messed up please help 0 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