- Home /
Error when trying to change color of gameobject
Hi i get this error when trying to change color of spriterenderer Assets\Scripts\Changecolour.cs(37,20): error CS0103: The name 'newColor' does not exist in the current context
This is my Script
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Changecolour : MonoBehaviour
{
public SpriteRenderer SpriteR;
private void Awake()
{
}
// Update is called once per frame
private void FixedUpdate()
{
if (SpriteR != null)
{
Debug.Log(SpriteR);
Color newColor = new Color(
Random.value,
Random.value,
Random.value
);
}
SpriteR = newColor;
}
}
Answer by GDGames0302 · Jan 30, 2021 at 05:02 PM
Hi. newColor is a local variable, which exists only in that if statement. You should declare that newColor at the top, under the public SpriteRenderer SpriteR. Or you can delete everything in the FixedUpdate and add only this: SpriteR.color = new Color(what values you want over here);
Answer by VeryAnnoyingCat · Jan 30, 2021 at 05:16 PM
Shove 'SpriteR = newColor;' into the if statement.
Your answer
![](https://koobas.hobune.stream/wayback/20220613024250im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
ReImport in c# of GameObjects only for Scene Objects, not assets? 1 Answer
How can I modify all fields of a Child Motion of a Blend Tree through Script? 1 Answer
how to move and jump at the same time,how to move and jump at the same time 0 Answers
Learn: Survival Shooter aim offset from mouse position 1 Answer
Another function for activating a different script 0 Answers