【问题标题】:Array Starts full and then empties数组开始满然后清空
【发布时间】:2022-07-07 23:22:13
【问题描述】:

我尝试了很多不同的方法。仅在编辑器中分配数组元素。使用 Await() 在启动时分配元素。更改脚本调用函数的内容。什么 GameObject 附带了脚本。如何调用 Vector2。数组是如何初始化的。我无法弄清楚我错过了什么。

游戏规则脚本

using System.Collections.Generic;
using UnityEngine;

public class GameRules : MonoBehaviour
{
    public GameObject[] rightSwitchSpawns = new GameObject[7];
    public GameObject[] leftSwitchSpawns = new GameObject[7];

    public GameObject rightSwitchPrefab;
    public GameObject leftSwitchPrefab;
    
    void Awake(){
        rightSwitchSpawns =  GameObject.FindGameObjectsWithTag("RightSpawns");
        leftSwitchSpawns = GameObject.FindGameObjectsWithTag("LeftSpawns");
    }
    // Start is called before the first frame update
    void Start()
    {
        RandomLeft();
        RandomRight();
    }

    public void RandomLeft()
    {   Debug.Log("This Left array length is " + leftSwitchSpawns.Length);
        int leftRandom1 = Random.Range(0, leftSwitchSpawns.Length -1);
        Debug.Log("left 1 index is " + leftRandom1);
        Vector2 leftOne = new Vector2(leftSwitchSpawns[leftRandom1].transform.position.x,leftSwitchSpawns[leftRandom1].transform.position.y);
        leftSwitchPrefab = Instantiate(leftSwitchPrefab, new Vector2(leftOne.x,leftOne.y), Quaternion.identity);

        int leftRandom2 = Random.Range(0, leftSwitchSpawns.Length -1);
        while(leftRandom2 == leftRandom1)
        {
            leftRandom2 = Random.Range(0, leftSwitchSpawns.Length -1);
        }
        Vector2 leftTwo = new Vector2(leftSwitchSpawns[leftRandom2].transform.position.x,leftSwitchSpawns[leftRandom2].transform.position.y);
        leftSwitchPrefab = Instantiate(leftSwitchPrefab, new Vector2(leftTwo.x,leftTwo.y), Quaternion.identity);
        Debug.Log("Left 2 index is: " + leftRandom2);

        int leftRandom3 = Random.Range(0, leftSwitchSpawns.Length -1);
        while(leftRandom3 == leftRandom1 || leftRandom3 == leftRandom2)
        {
            leftRandom3 = Random.Range(0, leftSwitchSpawns.Length -1);
        }
        Vector2 leftThree = new Vector2(leftSwitchSpawns[leftRandom3].transform.position.x,leftSwitchSpawns[leftRandom3].transform.position.y);
        leftSwitchPrefab = Instantiate(leftSwitchPrefab, new Vector2(leftThree.x,leftThree.y), Quaternion.identity);
        Debug.Log("Left 3 index is: " + leftRandom3);
    }

    public void RandomRight()
    {
        int rightRandom1 = Random.Range(0, rightSwitchSpawns.Length -1);
        Debug.Log("This Right array length is " + rightSwitchSpawns.Length);
        Vector2 rightOne = rightSwitchSpawns[rightRandom1].transform.position;
        rightSwitchPrefab = Instantiate(rightSwitchPrefab, new Vector2(rightOne.x,rightOne.y), Quaternion.identity);
        Debug.Log("Right 1 index is: " + rightRandom1);

        int rightRandom2 = Random.Range(0, rightSwitchSpawns.Length -1);
        while (rightRandom2 == rightRandom1)
        {
            rightRandom2 = Random.Range(0, rightSwitchSpawns.Length -1);
        }
        Vector2 rightTwo = rightSwitchSpawns[rightRandom2].transform.position;
        rightSwitchPrefab = Instantiate(rightSwitchPrefab, new Vector2(rightTwo.x,rightTwo.y), Quaternion.identity);
        Debug.Log("Right 2 index is: " + rightRandom2);

        int rightRandom3 = Random.Range(0, rightSwitchSpawns.Length -1);
        while (rightRandom3 == rightRandom1 || rightRandom3 == rightRandom2)
        {
            rightRandom3 = Random.Range(0, rightSwitchSpawns.Length -1);
        }
        Vector2 rightThree = rightSwitchSpawns[rightRandom3].transform.position;
        rightSwitchPrefab = Instantiate(rightSwitchPrefab, new Vector2(rightThree.x,rightThree.y), Quaternion.identity);
        Debug.Log("Right 3 index is: " + rightRandom3);
    }

    public void DestroyRightSwitches(){
        Debug.Log("Right Switches Destroyed");
        Destroy(rightSwitchPrefab);
    }

    public void DestroyLeftSwitches(){
        Debug.Log("Left Switches Destroyed");
        Destroy(leftSwitchPrefab);
    }
}

PlayerMovement 脚本

using System.Collections.Generic;
using UnityEngine;

public class PlayerMovement : GameRules
{
    private float speed = 5f;
    private float jump = 5f;
    private Rigidbody2D rb;
    private bool isPlaying = false;
    private Transform trans;

    // Start is called before the first frame update
    void Awake()
    {
        trans = gameObject.GetComponent<Transform>();
        rb = gameObject.GetComponent<Rigidbody2D>();
        rb.simulated = false;
        
    }

    // Update is called once per frame
    void Update()
    {
        if(Input.GetMouseButtonDown(0)){
            if(isPlaying == false){
                isPlaying = true;
                rb.simulated = true;
                rb.velocity = new Vector2(speed,0f); 
            }
            else{
                rb.velocity = new Vector2(speed,jump);
            }
        }
    }
    void OnCollisionEnter2D(Collision2D collision)
    {
        if (collision.gameObject.tag == "Wall")
        {
           OnDeath();
        }

        if(collision.gameObject.tag == "RightSwitch"){
            speed = -speed;
            rb.velocity = new Vector2(speed,0f); 
            DestroyRightSwitches();
            RandomRight();
        }
         if(collision.gameObject.tag == "LeftSwitch"){
            speed = -speed;
            rb.velocity = new Vector2(speed,0f); 
            DestroyLeftSwitches();
            RandomLeft();
        }
    }

    void OnDeath(){
        isPlaying = false;
        rb.simulated = false;
        trans.position = new Vector2(0,0);
        DestroyLeftSwitches();
        DestroyRightSwitches();
        RandomLeft();
        RandomRight();
    }
}

具体错误是

IndexOutOfRangeException:索引超出了数组的范围。 GameRules.RandomLeft () (在 Assets/Scripts/GameRules.cs:28) GameRules.Start () (在 Assets/Scripts/GameRules.cs:20)

但是,如果您查看调试语句。在初始运行时,数组已满,长度为 7,为您提供每个点的索引。然后它出于某种原因再次运行,长度和索引为 0。然后出现错误。

The errors I Get in picture form

【问题讨论】:

    标签: c# arrays unity3d


    【解决方案1】:

    您的数组被声明为public。在 Unity3d 的MonoBehaviour 中表示这些字段是序列化的。如果您将此脚本附加到场景中的游戏对象或某个之后实例化的预制件,则将从该游戏对象获取序列化值(您可以在检查器中看到它)。这意味着如果序列化对象中有空数组,则数组的长度在运行时将为0。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-06-08
      • 2013-06-27
      相关资源
      最近更新 更多