Hi! So I’m trying to make a genetic algorithm and I’m working on the gene generation, so I’m storing the genes into an array but when I try to add to the array it says the Array Index is Out of Range. I don’t have too much experience with arrays, so any help is much appreciated.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class DNA : MonoBehaviour {
public float geneNumber = 100f;
public Vector3[] genes;
void Start() {
GenerateDNA ();
}
public void GenerateDNA() {
for (int i = 0; i < 100; i++) {
genes[i] = new Vector3(Random.Range(0f, 5f), Random.Range(0f, 5f), 0f);
}
Debug.Log (genes);
}
}
Arrays don’t work that way, you have to specify a length first, then you can fill it
Thanks!
I turned it into public Vector3[] genes = new Vector3[10];
and that it accepts, but is there any way I can change it to like public Vector3[] genes = new Vector3[geneNumber];? Because it doesn’t accept that.
First of all, you should start actually learning C#, because it seems like you can’t correct basic errors
I can tell what your compiler says just by looking at the code, “Cannot implicitly convert type ‘float’ to ‘int’…”
Why on earth is your geneNumber a float? it’s a number of genes, so an integer, isn’t it?
Maybe you should not assume I dont know C#. I’ve worked with unity for a year on and off now. I’ve just never had to bother with arrays. Look, dude, if you aren’t going to help then go away.
Sorry, but to me someone who doesn’t know how to use an array, EVEN if you he didn’t need to use it, then he doesn’t know C#. It’s like the 3rd or 4th tutorial in every series I ever encountered. And if you read my question again, you will see that:
a.) I didn’t have a problem with you not knowing how to use an array, I had a problem with you asking about your error that is very basic and everyone should be able to fix it, especially if you used C# for a long time and you didn’t even post the error, meaning that you wanted us to be the compilers and tell you what to do
b.) I helped you
Sorry but to me someone who says “you should start actually learning C#” is a jerk. I converted it to an int and tried that as well but its telling me it can’t access something.
Everything I’ve used in Unity has been from tutorials on this site, tutorials on youtube, and the library you can access with cmd + '. I tried everything I could find and I’ve been unable to find an answer. So I came here looking for help, not for someone to treat me like an idiot.
And I came here to help people, not to get called a jerk and play a compiler from time to time
You have an error, copy-paste it here instead of saying “it can’t access something”
Just looking at the code its apparent whats else is going wrong
public Vector3[] genes = new Vector3[geneNumber];
at this point in the code you are still defining the variables. You can’t use variables to define other variables (e.g. you can’t use geneNumber here, hence the “can’t access something” error you’re likely getting). you’ll have to wait until you call that in a method before you can use that. However since you’ve made genes public and the original code is not even using geneNumber, there no reason for the class to even have it (as you can set the gene’s size in the inspector)
furthermore later on in the code at
for (int i = 0; i < 100; i++)
you assume the array size will always be 100 elements and this is the likely cause for your “Index is out of range” error. You should make this dynamic to the size of the array
for (int i = 0; i < genes.Length; i++)
Working in Unity, even for 5 years, does not mean you know C#. Even if you have written code, Unity hides a great deal of true C#.
secondly learning languages takes several years. looking back, knowing what I knew after my first year of programming is vastly different from what I know now after nearly ~15 years of programming. And I’m still learning. While you may not like Gorbits opinion(which to me was brutally honest, but not condesending) I do agree with his professional advice. arrays are an important building block in nearly every language so it is very important that you can understand how they work if you want to continue with a programming career.
Continue following those tutorials even those tutorials and Unite videos that bear little relevance to your work. they all have little nuggets of knowledge that can prove useful for you in the future, improve your understanding of Unity, or show you a new way of doing something your currently doing. Also look into StackOverflow they have great advice and are usually among the top hits of any google search relating to programming.
I didn’t try to be harsh, I wanted to give him a helpful advice. Maybe with the ‘actually’ placed in there it feels like it, but that wasn’t my intention at all
I’m not a professional to any degree, I probably wasn’t even living when you started with C#
I have only one problem with Unite videos, and that is that they are usually multiple hours long and because it was in a convention and it’s not a normal tutorial series, the pacing might be slow
Actually it works in this particular situation, since the array will be serialized and could have been set up using the inspector. Other than that it’s the loop which is actually the root of all evil like already mentioned.
If your gene length is going to change many times, with elements being added or removed every cycle of gene generation maybe, you might be better off using a list.