Error in Unity3D

-1

How can I fix this error? I did not find anything on the internet ... this is the script

'using UnityEngine; using UnityEngine.AI;

public class BlueCubeBehaviour: MonoBehaviour {

private Rigidbody BlueCubeMovement;

private NavMeshAgent FollowPlayer;

public Transform PlayerPosition;

private Renderer Appearance;

private bool ActiveNavMesh;

float Velocity = Random.Range(2.5f, 5.8f);

void Start()
{

    ActiveNavMesh = false;

    tag = "YellowEnemyCube";

    float ScaleOfCube = Random.Range(1.0f, 3.8f);

    BlueCubeMovement = GetComponent<Rigidbody>();

    Appearance = GetComponent<Renderer>();

    FollowPlayer = GetComponent<NavMeshAgent>();

    Appearance.material.color = Color.blue;

    transform.localScale = new Vector3(ScaleOfCube, ScaleOfCube, ScaleOfCube);

    BlueCubeMovement.mass = Random.Range(1.0f, 15.0f);

}

void Update()
{

    if (ActiveNavMesh == true) {

        FollowPlayer.destination = PlayerPosition.position;

        FollowPlayer.velocity = new Vector3(Velocity, Velocity, Velocity);

    }

}

private void OnCollisionEnter()
{
    if (BlueCubeMovement.angularVelocity == new Vector3(0, 0, 0)) {

        ActiveNavMesh = true;

    }

}

private void OnCollisionStay()
{
    if (BlueCubeMovement.angularVelocity == new Vector3(0, 0, 0)) {

        ActiveNavMesh = true;

    }

}

} 'this script is in a cube ... and enjoy the following error

    
asked by anonymous 11.11.2018 / 04:09

1 answer

1

This line is causing an error in your code because you are starting an object outside of a function.

float Velocity = Random.Range(2.5f, 5.8f);

The error itself says that you are assigning this variable in the instance variable initiator.

Do not assign any variables outside of Start (which Unity by default prompts you to start in it, which is the equivalent of the constructor), and your code will work.

Solution: Just create a variable outside of the functions and boot it into Start.

Remember, creating a variable and starting a variable are different things.

    
11.11.2018 / 04:38