TransWikia.com

Почему при написании кода на C# возникает куча ошибок?

Stack Overflow на русском Asked by Ryannesso on December 23, 2020

Я хочу сделать игру раннер.

Но когда пишу код для того чтобы мой персонаж двигался по дороге бесконечно в Юнити вылезает куча ошибок.

AssetsScriptsRoadSpawner.cs(15,27): error CS1061: 'List<GameObject>' does not contain a definition for 'OrderBy' and no accessible extension method 'OrderBy' accepting a first argument of type 'List<GameObject>' could be found (are you missing a using directive or an assembly reference?)

AssetsScriptsRoadSpawner.cs(21,31): error CS0119: 'RoadSpawner' is a type, which is not valid in the given context

AssetsScriptsRoadSpawner.cs(22,21): error CS0117: 'RoadSpawner' does not contain a definition for 'Remove'

AssetsScriptsRoadSpawner.cs(22,28): error CS0103: The name 'movedRoad' does not exist in the current context

AssetsScriptsRoadSpawner.cs(23,53): error CS0117: 'RoadSpawner' does not contain a definition for 'Count'

AssetsScriptsRoadSpawner.cs(23,22): error CS0021: Cannot apply indexing with [] to an expression of type 'method group'

Код TestCharController:

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

public class TestCharController : MonoBehaviour
{
    public float movementSpeed = 10f;

    // Start is called before the first frame update
    void Start()
    {
        
    }

    // Update is called once per frame
    void Update()
    {
        float hMovement = Input.GetAxis("Horizontal") * movementSpeed / 2;
        float vMovement = Input.GetAxis("Vertical") * movementSpeed;

        transform.Translate(new Vector3(hMovement, 0, vMovement) * Time.deltaTime);
    }
}

Код TestCameraController:

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

public class TestCameraController : MonoBehaviour
{
    private Transform player;

    private float yOffset = 1.5f;
    private float xOffset = -4f;
    private float zOffset = 0f;

    // Start is called before the first frame update
    void Start()
    {
        player = GameObject.Find("Player").transform;
    }

    // Update is called once per frame
    void Update()
    {
        transform.position = new Vector3(player.position.x + xOffset, player.position.y + yOffset, player.position.z + zOffset);
    }
}   

Код RoadSpawner:

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

public class RoadSpawner : MonoBehaviour
{
    public List<GameObject> roads;
    private float offset = 60f;

    // Start is called before the first frame update
    void Start()
    {
        if(roads != null && roads.Count > 0)
        {
            roads = roads.OrderBy(roads => r.transform.position.z).ToList();
        }
    }

    public void MoveRoad()
    {
        GameObject moveRoad = RoadSpawner[0];
        RoadSpawner.Remove(movedRoad);
        float newZ = RoadSpawner.Equals[RoadSpawner.Count - 1].transform.position.z + offset;
        moveRoad.transform.position = new Vector3(0, 0, newZ);
    roads.Add(moveRoad);
}
} 

Код SpawnManager:

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

public class SpawnManager : MonoBehaviour
{
    // Start is called before the first frame update
    void Start()
    {
        
    }

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

Помогите пожалуйста, я новичёк в создании игр и просто не понимаю что мне делать.

https://imgur.com/a/v01a08m

One Answer

Подключите Linq в RoadSpawner в самом начале.

using System.Linq;

Это что касается первой ошибки.

А остальные ошибки из-за непонимания основ.

RoadSpawner[0] не имеет смысла. Подозреваю, что вместо:

GameObject moveRoad = RoadSpawner[0];

Должно быть:

GameObject moveRoad = roads[0];

Answered by Suvitruf - Andrei Apanasik on December 23, 2020

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP