Ошибка cs0201 unity

Using Unity, I’m trying to add GameObjects with a particular script to an array. I’ve tried a bunch of different methods and ultimately I’ve ended up with this, and for the life of me I can’t find why it’s giving me the error, because everything I’ve googled for the last hour has said it’s mostly from syntax and I cannot find the syntax error. I wonder if it wouldn’t just make sense to use a get/set but I really don’t understand how those are different from for loops. Thank you!

public class QuestFinderScript : MonoBehaviour {
GameObject[] objects;
public List<GameObject> interactables = new List<GameObject> ();
int interactablesSize;

void Start(){

    interactables = new List<GameObject> ();
    objects = GameObject.FindGameObjectsWithTag ("Untagged");
    interactablesSize = objects.Length;

    for (int i = 0; i < interactablesSize; i++) {

        InteractionSettings iset = objects [i].GetComponent<InteractionSettings> ();
        if (iset != null) {
            interactables.Add [i];

        }
    }
}

}

asked Feb 23, 2017 at 20:19

jannar's user avatar

4

Thanks to your input, I’ve fixed it — part of the problem was that I didn’t realize the script I was referencing was a child, not a component of the gameobject itself. But if anyone’s curious:

    interactables = new List<GameObject> ();
    objects = GameObject.FindGameObjectsWithTag ("questable");
    interactablesSize = objects.Length;

    Debug.Log (interactablesSize);

    for (int i = 0; i < interactablesSize; i++) {

        InteractionSettings iset = objects [i].GetComponentInChildren<InteractionSettings> ();
        if (iset != null) {
            interactables.Add(iset.gameObject);
        }
    }

answered Feb 23, 2017 at 20:40

jannar's user avatar

jannarjannar

511 gold badge1 silver badge4 bronze badges

1st problem: As Pogrammer says, Add is a function for list, not an indexer.

Add(i) is correct

Of course, the error is due to your 1st problem.

2nd problem: Interactables os list of GameObject, you try to add integer(i is integer) to list of GameObjects.

Review your code and understand what you are going to add to Interactables.

answered Feb 23, 2017 at 20:32

Efe's user avatar

EfeEfe

80010 silver badges32 bronze badges

Добрый день, у меня возникла проблема с кодом (c#) для спрайта beetle. Я несколько раз перепроверил свой код, но все равно мне выскакивает ошибка:

AssetsScriptsBeetle.cs(42,9): error CS0201: Only assignment, call, increment, decrement, await, and new object expressions can be used as a statement

Буду рад, если поможете)

Мой код:

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

public class Beetle : MonoBehaviour
{
    public float speed = 4f;
    bool isWait = false;
    bool isHidden = true;
    public float waitTime = 4f;
    public Transform point;

    void Start()
    {
        point.transform.position = new Vector3(transform.position.x, transform.position.y + 1f, transform.position.z);
    }
    
    void Update()
    {
        if (isWait == false)       
            transform.position = Vector3.MoveTowards(transform.position, point.position, speed*Time.deltaTime);   
        if (transform.position == point.position)
        {
            if (isHidden)
            {
                point.transform.position = new Vector3(transform.position.x, transform.position.y + 1f, transform.position.z);
                isHidden = false;
            } else
            {
                point.transform.position = new Vector3(transform.position.x, transform.position.y - 1f, transform.position.z);
                isHidden = true;
            }

            isWait = true;
            StartCoroutine(Waiting());
        }
    }
    
    IEnumerator Waiting()
    {
        yield return new WaitForSeconds(waitTime);
        isWait == false;
    }   
}

C# Compiler Error

CS0201 – Only assignment, call, increment, decrement, and new object expressions can be used as a statement

Reason for the Error

You will receive this error when you have an invalid statement in your C# code. For example, you have an statement that end with a semicolon and doesnot have =, () , new — or ++ operation in it.

For example, try to compile the below code snippet.

namespace DeveloperPubNamespace
{
    class Program
    {
        static void Main(string[] args)
        {
            8 * 2;
        }
    }

}

This will result with the error code CS0201 as you have used 8 * 2 in a statement but doesnot contain any assignment operation.

Error CS0201 Only assignment, call, increment, decrement, await, and new object expressions can be used as a statement ConsoleApp3 C:UsersSenthilBalusourcereposConsoleApp3ConsoleApp3Program.cs 7 Active

C# Error CS0201 - Only assignment, call, increment, decrement, and new object expressions can be used as a statement

Solution

To fix the error code CS0201, you will need to ensure that the statement is valid. You can fix the above code by using the assignment operation.

namespace DeveloperPubNamespace
{
    class Program
    {
        static void Main(string[] args)
        {
            int i = 8 * 2;
        }
    }

}

Понравилась статья? Поделить с друзьями:
  • Ошибка cs0116 unity
  • Ошибка cs0104 unity
  • Ошибка cs0103 visual studio
  • Ошибка crycloud hunt showdown 0x60004
  • Ошибка cs0102 unity