Assigning Vector3 Array values in C#

Yes, this has been debated a lot, yet I’m unable to find a stup similar to mine, nor any help in the Unity documentation, because it’s pretty outdated on the C# Array stuff. I have filed this to Unity. In the meantime, I’m STUCK, oh my GOD I hate it, any pointer would be very appreciated, thanks :slight_smile:

using UnityEngine;
using System.Collections;

public class Star_Handler : MonoBehaviour
{	
		public GameObject[] stars;
		public Vector3[] starMov;

		void Start ()
		{
				stars = GameObject.FindGameObjectsWithTag ("Star");
				for (int i = 0; i<stars.Length; i++) {
						starMov  *= new Vector3 (1.2f, 1.2f, 1.2f);//this line is giving me an Array out of range - why?*
  •  		}*
    
  •  }*
    

}

1 Answer

1

You aren’t initializing starMov to anything, so starMov_, where i is anything will return array out of bounds._
Try this:
stars = GameObject.FindGameObjectsWithTag(“Star”);
starMov = new Vector3[stars.Length];
for(int i = 0; i < stars.Length; ++i){
starMov = new Vector3(1.2f, 1.2f, 1.2f);
}
Edit: I was overzealous with my parens, fixed now.

You are right - I should need to initialize it - I had tried that without success - and unfortunately your code does not do the trick ;/ Something is wrong with starMov = new Vector3stars.Length; it returns Expression denotes a value', where a method group' was expected and even if I replace it with say starMov = new Vector3[500); I still get the OutOfRange error!

the code you've posted in your comment makes no sense... is it copy/paste from your actual code, or are you retyping it in here? If that is the actual code you are using, you are messing up your braces. Look carefully at my code and your code. See how I use the square brackets around star.Length in the array initializer? See how the code you've posted does not?

OMG; you are right, I'm sorry, I was misreading you and basically going totally bra[e blind after pounding that same thing too long :) Thank you very much!!

"Edit: I was overzealous with my parens, fixed now." Lol - I thought I was going crazy :D Thanks a bunch!!

If its all working, don't forget to mark the answer ;)