Home > Software design >  Moving objects into random places by using functions
Moving objects into random places by using functions

Time:11-21

I'm writing a code of moving a object to random places. I made a function which decides the random coordinates and returns it. However, I think the function and is not connected together. This is what I tried...

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

public class Movement : MonoBehaviour
{

    public float speed;

    Vector3 target;

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

    // Update is called once per frame
    void Update()
    {
        Vector3 target = random(target);
        transform.position = Vector3.MoveTowards(transform.position, target, Time.deltaTime * speed);
    }

    Vector3 random(Vector3 target)
    {
        float min = -100.0f;
        float max = 100.0f;
        float randomX = Random.Range(min, max);
        float randomZ = Random.Range(min, max);
        Vector3 target = new Vector3(randomX, 10.0f, randomZ);

        return target;
    }

}

And this is the error message I got.

Assets\Movement.cs(31,17): error CS0136: A local or parameter named 'target' cannot be declared in this scope because that name is used in an enclosing local scope to define a local or parameter

How can I fix this?

CodePudding user response:

The problem is you define a new target variable inside random method. You have defined it as argument in method body before. Change one of them.

CodePudding user response:

It is not really a problem with unity but with compiler.

Inside the random(Vector3 target) and void Update() methods you are defining new "target" variables like this:

Vector3 target

so the compiler is telling you that this is not allowed. If you want to update the target variable then remove the "Vector3" infront or choose a new name.

It is not clear how this function is meant to work but I think it is good to declare the target on start or declare it public so you can assign a start position to it.

I think what you want is something like this:

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

public class Movement : MonoBehaviour
{

    public float speed;

    // OPTIONAL: declare public so the editor will let you set a position
    public Vector3 target;

    // Start is called before the first frame update
    void Start()
    {
        // OPTIONAL: you can set a start position
        target = Vector3.zero;
    }

    // Update is called once per frame
    void Update()
    {
        target = random(target);
        transform.position = Vector3.MoveTowards(transform.position, target, Time.deltaTime * speed);
    }

    Vector3 random(Vector3 par)
    {
        float min = -100.0f;
        float max = 100.0f;
        float randomX = Random.Range(min, max);
        float randomZ = Random.Range(min, max);
        return new Vector3(randomX, 10.0f, randomZ);
    }

EDIT: You do not really need a parameter to the "random" function now. So maybe you can remove it

CodePudding user response:

The reason you are getting this error is because you have already declared Vector3 target as a class member outside of the random and update methods.

If you are coming from a language like JavaScript, you can do it by declaring the same var variable name inside an inner scope but in C# it's not possible.

  • Related