Jump to content

Unity C# error CS0201

Lennart van de Merwe

Hi so my code is producing error CS0201: Only assignment, call, increment, await, and new object expressions can be used as a statement. It's located in line 15 (marked with //). As of now, my BCFC script contains a public static that I'm trying to refer to. So, what's wrong?

 

 

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

public class TestBackground : MonoBehaviour
{

    BCFC controller;
    public Texture tex;

    // Start is called before the first frame update
    void Start()
    {
        BCFC.instance; //this is line 15, and producing the error
    }

    // Update is called once per frame
    void Update()
    {
        BCFC.LAYER layer = null;
        if (Input.GetKey(KeyCode.Q))
            layer = controller.background;
        if (Input.GetKey(KeyCode.W))
            layer = controller.cinematic;
        if(Input.GetKey(KeyCode.A))
            layer = controller.foreground;

        if (Input.GetKey(KeyCode.T))
        {

        } else
        {
            if (Input.GetKeyDown(KeyCode.Z))
                layer.SetTexture(tex);
            
        }
    }
}


 

 

This originally wasn't my code and I'm still in the process of trying to understand, but I'll try to answer any questions you might have and/or provide extra code or info you might need.

Edited by Lennart van de Merwe
I'm rarted
Link to comment
Share on other sites

Link to post
Share on other sites

5 minutes ago, Lennart van de Merwe said:

As of now, my BCFC script contains a public static that I'm trying to refer to. So, what's wrong?



void Start()
{
   BCFC.instance; //this is line 15, and producing the error
}

 

Basically, what is happening is you're stating the name of a variable, but you're not trying to do anything with it.

To give a bit of a metaphor, doing this in code is basically like yelling someone's name at them, like "Steve!". 'Steve' will be very confused if you just say his name and nothing else "yes, you called my name, what about it? what do you want me to do?"

 

In programming, you don't just yell out the name of something. You do one of these things with it:

Quote

Only assignment, call, increment, await, and new object expressions can be used as a statement

Where did you get this code from?

"We're all in this together, might as well be friends" Tom, Toonami.

 

mini eLiXiVy: my open source 65% mechanical PCB, a build log, PCB anatomy and discussing open source licenses: https://linustechtips.com/topic/1366493-elixivy-a-65-mechanical-keyboard-build-log-pcb-anatomy-and-how-i-open-sourced-this-project/

 

mini_cardboard: a 4% keyboard build log and how keyboards workhttps://linustechtips.com/topic/1328547-mini_cardboard-a-4-keyboard-build-log-and-how-keyboards-work/

Link to comment
Share on other sites

Link to post
Share on other sites

5 minutes ago, minibois said:

Where did you get this code from?

A youtube tutorial on how to make a Visual Novel.

 

Quote

Basically, what is happening is you're stating the name of a variable, but you're not trying to do anything with it.

So I can just put "//" before it and hope for the best?
 

Link to comment
Share on other sites

Link to post
Share on other sites

Just now, Lennart van de Merwe said:

So I can just put "//" before it and hope for the best?

Maybe it will work, maybe not. Without knowing the context, I have no idea what "BCFC" is, what "BCFC.instance" is supposed to do, etc. 

"We're all in this together, might as well be friends" Tom, Toonami.

 

mini eLiXiVy: my open source 65% mechanical PCB, a build log, PCB anatomy and discussing open source licenses: https://linustechtips.com/topic/1366493-elixivy-a-65-mechanical-keyboard-build-log-pcb-anatomy-and-how-i-open-sourced-this-project/

 

mini_cardboard: a 4% keyboard build log and how keyboards workhttps://linustechtips.com/topic/1328547-mini_cardboard-a-4-keyboard-build-log-and-how-keyboards-work/

Link to comment
Share on other sites

Link to post
Share on other sites

1 minute ago, minibois said:

Only assignment, call, increment, await, and new object expressions can be used as a statement

To give a bit more context:

// assignment
variableName = someValue;

// call, e.g. a method
methodName();

// increment
variableName++;
variableName = variableName + 1;

// await, e.g. a method
await methodName();

// new object expression
variableName = new SomeObject();

 

1 minute ago, Lennart van de Merwe said:

So I can just put "//" before it and hope for the best?

If you don't know what the line is supposed to do, then sure.

Remember to either quote or @mention others, so they are notified of your reply

Link to comment
Share on other sites

Link to post
Share on other sites

15 minutes ago, minibois said:

Maybe it will work, maybe not. Without knowing the context, I have no idea what "BCFC" is, what "BCFC.instance" is supposed to do, etc. 

Ait,  I tried it,  too bad it didn't work. It would appear it's ought to do something.

Without "BCFC.instance" I just get NullRefException errors.

Here's my BCFC code, the goal of this code, and the tester, is to change the source image of a UI element (in order to change the background).
 

public class BCFC : MonoBehaviour
{
    public static BCFC instance;

    public LAYER background = new LAYER();
    public LAYER cinematic = new LAYER();
    public LAYER foreground = new LAYER();

    void Awake()
    {
        instance = this;
    }

    [System.Serializable]
    public class LAYER
    {
        public GameObject root;
        public GameObject newImageObjectReference;
        public RawImage activeImage;
        public List<RawImage> allImages = new List<RawImage>();

        public void SetTexture(Texture texture) 
        {
            if (texture != null)
            {
                if (activeImage == null)
                    CreateNewActiveImage();

                activeImage.texture = texture;
                activeImage.color = GlobalF.SetAlpha(activeImage.color, 1f);

     
            } else
            {
                if(activeImage != null)
                {
                    allImages.Remove(activeImage);
                    GameObject.DestroyImmediate(activeImage.gameObject);
                    activeImage = null;
                }
            }
        }

        void CreateNewActiveImage()
        {
            GameObject ob = Instantiate(newImageObjectReference, root.transform) as GameObject;
            ob.SetActive(true);
            RawImage raw = ob.GetComponent<RawImage>();
            activeImage = raw;
            allImages.Add(raw);
        }
    }
}

Maybe this can give some insight as to why I'm getting CS0201

Link to comment
Share on other sites

Link to post
Share on other sites

It looks like you're trying to create a singleton. Where exactly are you creating an instance of your class? (e.g. "new BCFC"?)

 

You're assigning "this" to "instance" in the "Awake()" method. Which means before Awake() is called (by the Unity framework, I assume?) the variable is still null and you can't use it to access your instance.

Remember to either quote or @mention others, so they are notified of your reply

Link to comment
Share on other sites

Link to post
Share on other sites

Why using static? They are bothersome in Unity and very inefficient. Try creating an instance of BCFC and use the script directly where ever you want. Also, why BFBC.instance? It doesn't do anything if the script isn't in the scene (ex, on a game manager object).

Link to comment
Share on other sites

Link to post
Share on other sites

17 hours ago, Guuhan said:

Why using static? They are bothersome in Unity and very inefficient. Try creating an instance of BCFC and use the script directly where ever you want. Also, why BFBC.instance? It doesn't do anything if the script isn't in the scene (ex, on a game manager object).

Thanks for the feedback, but I gotta be honest, I'm far from the level of C# where I can make actual decisions on how to do stuff best. Currently I'm following a tutorial, and trying to fix whatever error comes along, and so hopefully learning the language bit by bit. For the record, I've been doing C# for 5 days now, so maybe at some point in the future I can use your feedback...

Furthermore, BFBC is attached to a gameObject so it should be working.

Anyways, I managed to fix the error, and I think it should be working now.


UPDATE: It worked, I fixed it.

I changed it from:

BCFC.instance;

to

controller = BCFC.instance;

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×