C# Script Error "Introduce local for Color.red" (Terrible Tweeters Youtube Tutorial)

Hey everybody, how was your Christmas and New Year? So I recently redownloaded unity a couple weeks ago to check it out again. I am loving the engine so far, it has improved so much over the years. I started out with a basic unity tutorial on Youtube that explains how unity works and how to make your first game. However, after writing some code I was stumped at the errors I was getting. My goal at the moment is to make a simple 2D angry birds styled game dubbed “Terrible Tweeters” by the Youtuber. As of right now I am trying to get the player’s little bird to change color when I click on him. But for some reason my code for this won’t work:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Bird : MonoBehaviour
{
    // Start is called before the first frame update
    void Start()
    {
        GetComponent<Rigidbody2D>().isKinematic = true;
    }

    void OnMouseDown()
    {
        GetComponent<SpriteRenderer>().color = Color.red;
    }

    // Update is called once per frame
    void Update()
    {
       
    }
}

More specifically the line of code “GetComponent().color = Color.red;”. I am using the Visual Studio Community 2019 text editor, “The purple one” if you will. It shows a weird screw driver next to that line and says “Introduce local for ‘Color.red’ | Introduce local for all occurrences of ‘Color.red’ | Use expression body for methods”. I have no idea what these errors mean. I would really appreciate any help explaining how this works because as of right now, when I click on the main player bird, nothing happens. If you need more details, the tutorial I am using is:

And the time stamp for my issue is:

41:48

Any help is greatly appreciated. Thanks in advance. :slight_smile:

I don’t use Visual Studio Community, because my quota of terrible software is already filled to the brim by Unity, but:

That doesn’t sound like an error. It seems like it’s suggesting to make a local variable for Color.red for you, but that’s not an error, the code should compile. Does it not work in Unity?

1 Like

So if they aren’t errors then it must be unity itself. I have also tried regenerating my project files, restarting visual studio, restarting unity and still nothing. This is very frustrating. :frowning:

Oh and no, it does not work at all in unity. I am getting zero feedback when clicking the player bird. :frowning:

Have you attached the script to anything?

Is Unity showing any errors?

Pretty sure Acid Arrow is correct: this is just offering to refactor for you. It’s not what is preventing you from working necessarily. I mean it might be, but that would be orthogonal to the message above.

To help gain more insight into your problem, I recommend liberally sprinkling Debug.Log() statements through your code to display information in realtime.

Doing this should help you answer these types of questions:

  • is this code even running? which parts are running? how often does it run?
  • what are the values of the variables involved? Are they initialized?

Knowing this information will help you reason about the behavior you are seeing.

Unity is showing no errors at all. Now I don’t know much about unity but I am pretty sure I have attached my script to my player’s bird because the instructor(Youtuber) specifically told me to rename my script to “Bird” or else I will run into errors. Is there something I am missing? How can I make sure I know what my script is attached too?

I followed your advice an added the Debug.Log() statements to my code:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Bird : MonoBehaviour
{
    // Start is called before the first frame update

    Debug.Log();

    void Start()
    {
        GetComponent<Rigidbody2D>().isKinematic = true;
    }

    Debug.Log();

    void OnMouseDown()
    {
        GetComponent<SpriteRenderer>().color = Color.red;
    }

    Debug.Log();

    // Update is called once per frame
    void Update()
    {
       
    }
}

However, when I run my code I get a message from unity saying that all my compiler issues must be resolved before I can enter playmode. So my game physically can’t run anymore. Is there something wrong with my code? Also there is proof that my code is very well running. In the tutorial you are asked to write a line of code that will keep the bird flying idle in the air regardless of the stimulated box being check marked. The line is “GetComponent().isKinematic = true;”. After typing out that line, the code for me successfully works and my bird stays in the air. So my code is most definitely running. I have no idea what could be the problem. It has to be unity itself at this point right? Maybe visual studio community(purple) is to blame. Should I try visual studio code(blue) instead?

That’s because you have put all of the calls to Debug.Log outside of any methods. You also are not using the Debug.Log correctly anyway as you are supposed to provide a value for it to actually log, eg:
Debug.Log("in Start method now");;
So first thing to do is to put each of the calls to Debug.Log() inside your methods (Start, OnMouseDown, Update), and include the text that you want it to log in the calls to the Debug.Log method.

Does your GameObject have a Collider? It needs one for OnMouseDown to work.
The method is called when you click on the object’s collider, not its sprite.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Bird : MonoBehaviour
{
    // Start is called before the first frame update

    void Start()
    {
        Debug.Log("in Start method now");
        GetComponent<Rigidbody2D>().isKinematic = true;
    }

    void OnMouseDown()
    {
        Debug.Log("in OnMouseDown method now");
        GetComponent<SpriteRenderer>().color = Color.red;
    }

    // Update is called once per frame
    void Update()
    {
        Debug.Log("in Update method now");
    }
}

Is this how your suppose use the Debug.Log command? I am not getting any complier errors anymore and my game runs just fine. So how exactly is this Debug.Log command going to help me find the solution to my problem? What exactly does this do? I don’t see anything in the console.

Yes I have a Rigidbody 2D and a polygon collider 2D attached to my player bird object.

Your base class is not actually being called maybe because you are lacking some unity extension in visual studio. I faced a similar problem until I realized there was a unity extension lacking in my visual studio. Earlier, before attaching that unity extension, the base class “MonoBehaviour” wasn’t even called in my code as it didn’t change its color also the “using UnityEngine;” wasn’t being called which included the base class MonoBehaviour. I am still not able to change the bird’s color on mouse down though lol. However, at least I was able to fix that extension issue which can help you through. Check out his other video which is almost the same which is:
https://www.youtube.com/watch?v=OR0e-1UBEOU
he didn’t use this “GetComponent().isKinematic = true;” in this. Try referring to the above video, might help.