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 marccrobbins · Nov 10, 2014 at 05:28 PM · c#sprite

The variable has not been assigned

Unity is telling me that "spriteRenderer" under void DoTheThing(){ has not been assigned but I have it assigned. Am I missing something?

 public Animator myAnim;
 //private string[] myArray = {"PhaseThru", "ExtraWarning"};
 [HideInInspector]
 public SpriteRenderer spriteRenderer;

 
 public virtual void start()
 {
             if (GetComponent<SpriteRenderer> ()) {
                     // save the current sprite as the up sprite
                     spriteRenderer = GetComponent<SpriteRenderer> () as SpriteRenderer;
             }
     }
 public void DoTheThing () {

     spriteRenderer.enabled = true;
     myAnim.SetBool("Warning", true);
     Debug.Log("Doing the thing!");
 }
 
 // Update is called once per frame
 void Update () {
 
 }

}

Comment
Add comment · Show 6
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 AlwaysSunny · Nov 10, 2014 at 05:17 PM 0
Share

What's with the virtual start() method? If you're expecting that to automatically invoke, use void Start() ins$$anonymous$$d.

Also, it's safe to assign directly to the variable, even if the result is null. So the if-block in the start method isn't necessary. (It's null to begin with, so assigning null again won't hurt)

 void Start() {
 spriteRenderer =  GetComponent<SpriteRenderer>() as SpriteRenderer;
 }
avatar image marccrobbins · Nov 10, 2014 at 05:28 PM 0
Share

Okay I did that but i'm still getting the spriteRenderer variable has not been assigned error.

avatar image AlwaysSunny · Nov 10, 2014 at 05:30 PM 0
Share

Are you sure there's a SpriteRenderer component attached to the object that has this script? Are you calling DoTheThing before Start() can invoke? Read the docs on Start() here http://docs.unity3d.com/ScriptReference/$$anonymous$$onoBehaviour.Start.html

avatar image Kiwasi · Nov 10, 2014 at 05:40 PM 0
Share

Variable not assigned is a warning, you can typically ignore it. I would not expect it with this code, unless Unity and $$anonymous$$onoDevelop are out of sync. If this is the case simply right clock on your script and choose sync $$anonymous$$onoDevelop project.

If spriteRenderer was not getting a value you would see a null reference exception when you called DoTheThing.

Couple of other notes, technically speaking there is nothing wrong with declaring Start as virtual or public. Public is generally bad practice, as you shouldn't call Start on your own. But there is nothing inherently wrong. There was a bug at one stage in Unity where the engine got confused about what to call if you overrode one of the default methods. But I believe that was fixed some time ago.

There is also no need to cast the GetComponent result. Its already returning a SpriteRenderer. Again nothing to do with your actual problem.

avatar image Kiwasi · Nov 10, 2014 at 05:41 PM 0
Share

If order of calling is your problem simply change Start to Awake. Not sure this will help though.

Show more comments

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by Baste · Nov 10, 2014 at 05:33 PM

You need to capitalize Start. The method named 'start' will never be run.

As long as you have a Spriterenderer attached to the same object, this code:

 void Start() {
     spriteRenderer =  GetComponent<SpriteRenderer>();
 }

will work. Note that with the generic version (), you won't need the cast ("as SpriteRenderer")

Comment
Add comment · Show 1 · 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 Kiwasi · Nov 10, 2014 at 05:43 PM 0
Share

Lol. $$anonymous$$icking myself over this one.

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Sprite Swapping help 1 Answer

Saving Rotation of Sprite 1 Answer

Unity streching sprite gameobject to fit two positions. 1 Answer


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