Not sure why this hapening.

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

public class RandomParkour : MonoBehaviour
{
    int x = 0;
    int z = 0;
 
    float CoinX = 0.0f;
    float CoinZ = 0.0f;
    int CoinSpawn = 0;

    int PrevX = 0;
    int PrevZ = 0;

    System.Random random = new System.Random();

    public GameObject Platform;
    public GameObject Player;
    public GameObject Coin;

    void Update()
    {

        if ( Player.transform.position.y > transform.position.y - 5 )
        {
            do
            {
                x = random.Next(-4, 4);
            }
            while ( (x == PrevX) || (x == PrevX - 1) || (x == PrevX + 1));

            do
            {
                z = random.Next(-4, 4);
            }
            while ( (z == PrevZ) || (z == PrevZ - 1) || (z == PrevZ + 1));

            CoinSpawn = random.Next(1, 10);
            if (CoinSpawn == 1)
            {
                System.Random random = new System.Random();

                CoinX = transform.position.x;
                CoinZ = transform.position.z;

                Coin.transform.position = new Vector3(CoinX, transform.position.y + 1, CoinZ);
                GameObject duplicate = Instantiate(Coin);
            }

            transform.position = new Vector3( x , transform.position.y + 2 , z );
             
            Platform.transform.position = transform.position;
            GameObject duplicate = Instantiate(Platform);
         
            PrevX = x;
            PrevZ = z;
        }
    }
}

so the code on line 55 works, the code on line 49 doesn’t! anyone know why?

And yes, I know the code is messy but oh well.

And the error code if it helps in any way.
Assets\Scripts\RandomParkour.cs(50,28): error CS0136: A local or parameter named ā€˜duplicate’ cannot be declared in this scope because that name is used in an enclosing local scope to define a local or parameter

You can’t use the same name ā€œduplicateā€ for both of those variables. You don’t even use either one of those variables so you can just get rid of the local variable declarations entirely. If you do want to use them, just give them different names.

2 Likes

@PraetorBlue
would it be possible for you to give an example piece of code as I can’t quite understand what you mean.

You are declaring the same variable, ā€œduplicateā€, twice. When you write ā€œGameObject duplciateā€, you are declaring a variable of the type ā€œGameObjectā€ called "duplicate and you cannot this more than once for the same variable in the same scope, which the code between { and }. Nested scopes inherit everything from their outer scope(s).

But in your code you are assigning a value to ā€œduplicateā€ but is not using it for anything, so you can just remove the assignment. You’re also modifying the values of the prefab before instantiating it, this ā€œworksā€ but it’s bad practice: you should assign the result of Instantiate() into a variable and modify the values of that instead:

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    
    public class RandomParkour : MonoBehaviour
    {
        int x = 0;
        int z = 0;
    
        float CoinX = 0.0f;
        float CoinZ = 0.0f;
        int CoinSpawn = 0;
    
        int PrevX = 0;
        int PrevZ = 0;
    
        System.Random random = new System.Random();
    
        public GameObject Platform;
        public GameObject Player;
        public GameObject Coin;
    
        void Update()
        {
    
            if ( Player.transform.position.y > transform.position.y - 5 )
            {
                do
                {
                    x = random.Next(-4, 4);
                }
                while ( (x == PrevX) || (x == PrevX - 1) || (x == PrevX + 1));
    
                do
                {
                    z = random.Next(-4, 4);
                }
                while ( (z == PrevZ) || (z == PrevZ - 1) || (z == PrevZ + 1));
    
               
                CoinSpawn = random.Next(1, 10);
                if (CoinSpawn == 1)
                {
                    System.Random random = new System.Random();
    
                    CoinX = transform.position.x;
                    CoinZ = transform.position.z;
    
                    GameObject coinDuplicate = Instantiate(Coin);
                    coinDuplicate.transform.position = new Vector3(CoinX, transform.position.y + 1, CoinZ);
                }
    
                transform.position = new Vector3( x , transform.position.y + 2 , z );
                
                GameObject platformDuplicate = Instantiate(Platform);
                platformDuplicate.transform.position = transform.position;
            
                PrevX = x;
                PrevZ = z;
            }
        }
    }
1 Like