How do I stop an array through a function?

Asked

Viewed 61 times

1

I am writing a code in Unity you need to pass several values through a function, I decided to use the array in the function statement, however when I try to recover the values of the array all of them are defined as 0, the code is as follows

public class ArrayTest : MonoBehaviour{

private int[] number;

void Start(){
    number = new int[] { 2, 3, 5 };
    Numbers(number);
}

void Numbers(int[] numb) { 
    numb = new int[3];

    Debug.Log("Este é o primeiro valor: " + numb[0]); 
    Debug.Log("Este é o segundo valor: " + numb[1]);
    Debug.Log("Este é o terceiro valor: " + numb[2]);
  }
}
  • Did the answer solve your question? Do you think you can accept it? See [tour] if you don’t know how you do it. This would help a lot to indicate that the solution was useful for you. You can also vote on any question or answer you find useful on the entire site (when you have 15 points).

1 answer

1

What you are doing does not make any sense. You receive a parameter and the first thing you do with it is to throw away its value and create another value. Or do something with the parameter or do something with the local variable you want.

Only remove the line that resets the parameter numb (bad name) within Numbers() that will work.

The code seems to have other problems, even if it works, but I can’t say anything because I don’t know what you’re trying to do.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.