- Home /
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 () {
}
}
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;
}
Okay I did that but i'm still getting the spriteRenderer variable has not been assigned error.
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
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.
If order of calling is your problem simply change Start to Awake. Not sure this will help though.
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")
Your answer
![](https://koobas.hobune.stream/wayback/20220613171156im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
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