Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Can C# have local variables with the same name?

Discussion in 'Scripting' started by keenanwoodall, Aug 3, 2014.

  1. keenanwoodall

    keenanwoodall

    Joined:
    May 30, 2014
    Posts:
    595
    I've been following a tutorial that is in C# and converting it to javascript. There's a section of the script that gives errors in javascript, for good reason! In the C# script the variables x, y, and z are declared twice in separate loop statements. I don't know why that would work. It sure doesn't work in javascript! I put an underscore before the second set of duplicate variables but my script didn't work. I'm not sure if its because the variables need to be the same or not. Here's the section from script in the tutorial:
    Code (csharp):
    1.  
    2. void Start () { data = new byte[worldX,worldY,worldZ];
    3. for (int x=0; x<worldX; x++)
    4. {
    5. for (int z=0; z<worldZ; z++)
    6. {
    7. int stone=PerlinNoise(x,0,z,10,3,1.2f);
    8. stone+= PerlinNoise(x,300,z,20,4,0)+10;
    9. int dirt=PerlinNoise(x,100,z,50,3,0)+1;
    10. for (int y=0; y<worldY; y++)
    11. {
    12. if(y<=stone)
    13. {
    14. data[x,y,z]=1;
    15. }
    16. else if(y<=dirt+stone)
    17. {
    18. data[x,y,z]=2;
    19. }
    20. }
    21. }
    22. }
    23. chunks=new GameObject[Mathf.FloorToInt(worldX/chunkSize),Mathf.FloorToInt(worldY/chunkSize),Mathf.FloorToInt(worldZ/chunkSize)];
    24. for (int x=0; x<chunks.GetLength(0); x++)
    25. {
    26. for (int y=0; y<chunks.GetLength(1); y++)
    27. {
    28. for (int z=0; z<chunks.GetLength(2); z++)
    29. {
    30. chunks[x,y,z]= Instantiate(chunk,new Vector3(x*chunkSize,y*chunkSize,z*chunkSize),new Quaternion(0,0,0,0)) as GameObject;
    31. Chunk newChunkScript= chunks[x,y,z].GetComponent(\"Chunk\") as Chunk;
    32. newChunkScript.worldGO=gameObject;
    33. newChunkScript.chunkSize=chunkSize;
    34. newChunkScript.chunkX=x*chunkSize;
    35. newChunkScript.chunkY=y*chunkSize;
    36. newChunkScript.chunkZ=z*chunkSize;
    37. }
    38. }
    39. }
    40. }
     
  2. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,398
    C# uses scope differently than JS. Scope is one of the things that Unityscript actually has in common with web Javascript.

    --Eric
     
  3. keenanwoodall

    keenanwoodall

    Joined:
    May 30, 2014
    Posts:
    595
    So how do you suggest I handle this problem?
     
  4. GarthSmith

    GarthSmith

    Joined:
    Apr 26, 2012
    Posts:
    1,240
    In C#, the integer 'x' only exists inside the for loop. Once the loop is over, there is no such thing as x anymore. So we have to create a new 'x' for every for loop.

    If I get this right, in UnityScript 'x' continues to exist after the for loop. So you just need to reset it, not make another variable 'x'

    After you declare x, y, and z the first time, just reset their values in following loops. By taking out "int" we are no longer declaring a new 'x' but changing the value of a previously existing 'x'.
    Code (csharp):
    1. // The FIRST time:
    2. for (int x = 0; ...; ...)
    3. // Anytime after that
    4. for (x = 0; ...; ...)
     
  5. keenanwoodall

    keenanwoodall

    Joined:
    May 30, 2014
    Posts:
    595
    Thanks, it works now but my script(which is creating a voxel terrain same as minecraft) only creates one chunk. I'm not sure what its related to
     
  6. keenanwoodall

    keenanwoodall

    Joined:
    May 30, 2014
    Posts:
    595
    nevermind, my variable was set to only make one. everything works perfectly! :)
     
    GarthSmith likes this.