简体   繁体   English

Unity中的运行时错误可能会破坏随机方向变化

[英]Run time error in Unity potentially breaking random direction change

Good day 美好的一天

I am writing one of my first unity programs, and I am getting an error that does not make sense to me. 我正在编写我的第一个统一程序,但遇到一个对我来说没有道理的错误。 I have a character on the screen that I move in a random direction when the game starts. 游戏开始时,我在屏幕上有一个随机移动的角色。 The character always seems to move in the same direction however despite the fact that I am randomizing the value. 尽管我正在随机分配值,但角色似乎总是朝着同一方向移动。

I intend to change the direction randomly as soon as a limit is reached as well, but this does not seem to work either. 我打算在达到极限后立即随机更改方向,但这似乎也不起作用。 The direction never seems to change, and I am wondering if it is due to an run time error that I am receiving. 方向似乎从未改变,我想知道这是否是由于我收到的运行时错误所致。

The error is telling me that the RigidBody variable inside my class is not set. 错误告诉我我的类中的RigidBody变量未设置。 I know I set it, and the fact that my character moves tells me it is finding the rigid body component. 我知道我已经设置好了,而且角色移动的事实告诉我它正在找到刚体部件。

My code looks like this: 我的代码如下所示:

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

public class Unit : MonoBehaviour {

    // Use this for initialization
    int direction;
    public Rigidbody2D rb;
    int speed = 5;
    int maxX = 6;
    int minX = -6;
    int maxY = 4;
    int minY = -4;
    void Start () {
        randomDirection();
    }

    void randomDirection()
    {
        System.Random rand = new System.Random(Guid.NewGuid().GetHashCode());

        int temp = -1;

        while(temp != direction)
        {
            temp = rand.Next(0, 4);
        }

        direction = temp;

        switch (direction)
        {
            case 0:
                rb.velocity = new Vector2(-speed, 0);
                break;
            case 1:
                rb.velocity = new Vector2(speed, 0);
                break;
            case 2:
                rb.velocity = new Vector2(0, -speed);
                break;
            case 3:
                rb.velocity = new Vector2(0, speed);
                break;
        }
    }

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

        if (transform.position.x >= maxX || transform.position.y >= maxY || transform.position.y <= minX || transform.position.y <= minY)
        {
            randomDirection();           
        }

        Vector2 pos = transform.position;
        pos.x = Mathf.Clamp(pos.x, -maxX, maxX);
        pos.y = Mathf.Clamp(pos.y, -maxY, maxY);
        transform.position = pos;
    }
}

Here is a screenshot of my inspector showing that rb (my RigidBody variable) is set: 这是检查器的屏幕截图,显示设置了rb(我的RigidBody变量): 在此处输入图片说明

Here is the error that I am receiving: 这是我收到的错误:

UnassignedReferenceException: The variable rb of Unit has not been assigned. UnassignedReferenceException:未分配Unit的变量rb。 You probably need to assign the rb variable of the Unit script in the inspector. 您可能需要在检查器中分配Unit脚本的rb变量。 UnityEngine.Rigidbody2D.set_velocity (Vector2 value) (at C:/buildslave/unity/build/artifacts/generated/common/modules/Physics2D/Physics2DBindings.gen.cs:2060) Unit.randomDirection () (at Assets/Scripts/Unit.cs:36) Unit.Start () (at Assets/Scripts/Unit.cs:17) UnityEngine.Rigidbody2D.set_velocity(Vector2值)(在C:/buildslave/unity/build/artifacts/Generated/common/modules/Physics2D/Physics2DBindings.gen.cs:2060)Unit.randomDirection()(在Assets / Scripts / Unit处) .cs:36)Unit.Start()(位于Assets / Scripts / Unit.cs:17)

I am not sure if I am checking whether the limit is being reached correctly. 我不确定是否正在检查是否已正确达到限制。 I am using the clamp command to restrict the unit to the limits of my screen. 我正在使用钳位命令将单位限制为屏幕的限制。 I put a debug in the if statement that checks my limit, and it seems to reach it, but still no direction change. 我在if语句中放入了一个调试命令,该命令检查了我的限制,并且似乎已经达到了限制,但是方向仍然没有改变。

Any advice would be greatly appreciated. 任何建议将不胜感激。 Thanks in advance! 提前致谢!

You get this error when your script is likely attached to multiple GameObjects or the-same GameObject twice and the variable is not assigned in the other object is is attached to. 当您的脚本很可能两次附加到多个GameObject或相同的GameObject,并且未附加在另一个对象中分配的变量时,会出现此错误。

With your screenshot, it is attached to the-same GameObject twice. 使用您的屏幕截图,它将两次附加到相同的GameObject。 The first one has a Rigidbody plugged into the rb variable slot. 第一个将Rigidbody插入rb可变插槽。 The second one doesn't. 第二个没有。 Remove the one Unit script which is above the Rigidbody component as that's the one that doesn't have anything assigned to the rb variable. 删除Rigidbody组件上方的一个Unit脚本,因为该脚本没有为rb变量分配任何内容。

This is how to remove it: 这是删除它的方法:

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM