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

What is the difference on these two code?

Discussion in 'Scripting' started by muhammet12, Jun 5, 2020.

  1. muhammet12

    muhammet12

    Joined:
    Feb 29, 2020
    Posts:
    10
    Hello, i using these two code,

    Code (CSharp):
    1.             for (int i = 0; i < pop; i++) {
    2.                 GameObject clone = (GameObject)Instantiate (arac, baslangic.transform.position, arac.transform.rotation);
    3.                 clone.AddComponent<aracKod>();
    4.                 clone.transform.SetParent (hareket.transform);
    5.                 clone.GetComponent<aracKod>().katmanlaraDegerAta(100);
    6.                 clone.GetComponent<aracKod>().carpma = 0;
    7.             }

    katmanlaraDegerAra(100) assign random values to variables and it is works perfect

    Code (CSharp):
    1.             for(int i  =0;i<pop;i++) {
    2.                 GameObject clone2 = (GameObject)Instantiate(arac,baslangic.transform.position,arac.transform.rotation);
    3.                 clone2.transform.SetParent (hareket.transform);
    4.                 clone2.AddComponent<aracKod>();
    5.              
    6.                 float[,] tmp1 = bestKatman1;
    7.                 clone2.GetComponent<aracKod>().girisKatman = tmp1;
    8.  
    9.                 float[,] tmp2 = bestKatman2;
    10.                 clone2.GetComponent<aracKod>().araKatman1 = tmp2;
    11.  
    12.                 float[,] tmp3 = bestKatman3;
    13.                 clone2.GetComponent<aracKod>().araKatman2 = tmp3;
    14.  
    15.                 float[,] tmp4 = bestKatman4;
    16.                 clone2.GetComponent<aracKod>().cikisKatman = tmp4;
    17.              
    18.                 clone2.GetComponent<aracKod>().katmanlaraDegerAta(100);
    19.                 clone2.GetComponent<aracKod>().carpma = 0;
    20.             }
    but when I use this , all instantiated object get same values not random. I am working on this for 2 days and can not find the problem.


    This is the randomize code but they are any changing on them.

    Code (CSharp):
    1.     public void katmanlaraDegerAta(float oran) {
    2.         girisKatman = katmanDegerDegis(girisKatman,oran);
    3.         araKatman1 = katmanDegerDegis(araKatman1,oran);
    4.         araKatman2 = katmanDegerDegis(araKatman2,oran);
    5.         cikisKatman = katmanDegerDegis(cikisKatman,oran);
    6.    
    7.     }
    8.  
    9.     float[,] katmanDegerDegis(float[,] agirlik,float oran) {
    10.    
    11.         float[,] tmpCikis = agirlik;
    12.         if(oran > 1) {
    13.             for(int i = 0;i<agirlik.GetLength(0);i++) {
    14.                 for(int j = 0;j<agirlik.GetLength(1);j++) {
    15.                
    16.                     tmpCikis[i,j] = Random.Range(-1.0f,1.0f);
    17.                 }
    18.             }
    19.         }
    20.         else {
    21.            
    22.             for(int i = 0;i<agirlik.GetLength(0);i++) {
    23.                 for(int j = 0;j<agirlik.GetLength(1);j++) {
    24.                     float ustLimit = Mathf.Clamp((agirlik[i,j] + oran),-1f,1f);
    25.                     float altLimit = Mathf.Clamp((agirlik[i,j] - oran),-1f,1f);
    26.                
    27.                     tmpCikis[i,j] = Random.Range(altLimit,ustLimit);
    28.                 }
    29.             }
    30.         }
    31.  
    32.         return tmpCikis;
    33.     }
     
    Last edited: Jun 5, 2020
  2. WarmedxMints

    WarmedxMints

    Joined:
    Feb 6, 2017
    Posts:
    1,035
    Without knowing what variable that method sets a random value on, it's impossible to say.

    Also, addcomponent returns the component so you could store it then and there would be no need to look it up so many times.
     
  3. muhammet12

    muhammet12

    Joined:
    Feb 29, 2020
    Posts:
    10
    I added the randomizing functions
     
  4. WarmedxMints

    WarmedxMints

    Joined:
    Feb 6, 2017
    Posts:
    1,035
    An array is a reference type and you are passing a reference to your 4 arrays in the second for loop.