Code (CSharp): public int[] w; void Start(){ w=new int[5];//so it will set size of w to 5 at the start } but i want to do it via a function like this Code (CSharp): public void setSizeOfArrayInt(int[] varName,int size){ varName= new int[size]; } but it's not working ,how can i fix it? setSizeOfArrayInt(w,5);
Passing w into a function only passes the value through in a temporary local variable 'varName'. If you want to assign a variable outside of the function scope as a parameter, you need to use the 'ref' or 'out' keywords for the parameter, to pass w as a reference. Use 'ref' if the variable is initialized outside the function, use 'out' if the variable should be initialized inside the function. Code (CSharp): private void Start() { int[] w; setSizeOfArray(out w, 5); } public void setSizeOfArray(out int[] varName, int size) { varName = new int[size]; }