Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Help with Error CS0131

Discussion in 'Scripting' started by mohasali, Feb 9, 2020.

  1. mohasali

    mohasali

    Joined:
    Feb 4, 2020
    Posts:
    3
    I am trying to add a value to a variable in a if statement but it gives me a CS0131 Error.
    Code:


    private void Update() {
    player = GameObject.Find("Player").transform.position;
    g=Vector3.Distance(player, lastEndPosition);
    Debug.Log(g);
    if (Vector3.Distance(player, lastEndPosition) < pD) {
    SpawnLevelPart();
    SpawnLevelPart();
    SpawnLevelPart();
    SpawnLevelPart();
    pD=pD+65;
    }

    }
     
  2. csofranz

    csofranz

    Joined:
    Apr 29, 2017
    Posts:
    1,556
    Please use code tags so we can better read the code
    Please indicate which line number the error happens so we know where to look
    Please quote the full error text (e.g. "The left-hand side of an assignment must be a variable, property or indexer"), not just the type, so we know what to look for
    Please also show the variable type definitions so we can properly analyse what is going on
     
    TaleOf4Gamers likes this.
  3. mohasali

    mohasali

    Joined:
    Feb 4, 2020
    Posts:
    3
    Sorry I am new to this . The Error said "error CS0131: The left-hand side of an assignment must be a variable, property or indexer"

    The whole Script:

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

    public class LevelGenerator : MonoBehaviour {

    private const float pD = 165f;

    [SerializeField] private Transform levelPart_Start;
    [SerializeField] private List<Transform> levelPartList;
    private Vector3 player;
    public Vector3 plus;
    private Vector3 lastEndPosition;
    private float g;


    private void Awake() {
    lastEndPosition = levelPart_Start.Find("End").position + plus;
    player = GameObject.Find("Player").transform.position;
    int startingSpawnLevelParts = 5;
    for (int i = 0; i < startingSpawnLevelParts; i++) {
    SpawnLevelPart();
    }
    }

    private void Update() {
    player = GameObject.Find("Player").transform.position;
    g=Vector3.Distance(player, lastEndPosition);
    Debug.Log(g);
    if (Vector3.Distance(player, lastEndPosition) < pD) {
    SpawnLevelPart();
    SpawnLevelPart();
    SpawnLevelPart();
    SpawnLevelPart();
    pD=pD+65;
    }

    }

    private void SpawnLevelPart() {
    Transform chosenLevelPart = levelPartList[Random.Range(0, levelPartList.Count)];
    Transform lastLevelPartTransform = SpawnLevelPart(chosenLevelPart, lastEndPosition);
    lastEndPosition = lastLevelPartTransform.Find("End").position + plus;
    }


    private Transform SpawnLevelPart(Transform levelPart, Vector3 spawnPosition) {
    Transform levelPartTransform = Instantiate(levelPart, spawnPosition, Quaternion.identity);
    return levelPartTransform;
    }

    }
     
  4. Hikiko66

    Hikiko66

    Joined:
    May 5, 2013
    Posts:
    1,303
    Code Tags
    https://forum.unity.com/threads/using-code-tags-properly.143875/

    pD is declared as a const.
    Code (CSharp):
    1. private const float pD = 165f;
    That stands for constant, it means you aren't allowed to change it's value.

    Which means you can't assign it a new value like you're trying to do here

    Code (CSharp):
    1. pD=pD+65;
    Take const out of its declaration if you are going to change it's value.
    Code (CSharp):
    1. private float pD = 165f;
     
  5. mohasali

    mohasali

    Joined:
    Feb 4, 2020
    Posts:
    3
    Thx for help it worked!!:D