Array problem, I'm novice
`Hi, I'm trying to make array, I have a transform in game and I want to make array with my prefabs and then when I press button, these prefabs will create as child of the my transform. Please post sample code. Thank you. I tried something but it didn't work. I was looking at unity scripts, but I don't understand how can I make what I want.
public GameObject[] videos;
public int numberOfVideos = 5;
public string nameOfVideo;
public GameObject video;
// Use this for initialization
void Start () {
rs = GetComponent<RecordingSys> ();
videos = new GameObject[numberOfVideos];
}
// Update is called once per frame
void Update () {
for (int i = 0; i < numberOfVideos; i++) {
GameObject go = Instantiate (video, new Vector3 ((float)i, 1, 0), Quaternion.identity) as GameObject;
go.transform.localScale = Vector3.one;
go.transform.parent = GameObject.Find ("VideoPanel2").transform;
videos[i] = go;
}
You tried something, but it didn't work; what did you try?
I'm not asking to be mean or anything. Each person has their own approach to solving a problem, so if you can show us what your approach was, it can give others an idea of how a solution can be presented in a way which improves your understanding as a whole.
Getting a solution from others, especially when considering yourself to be a "novice" can easily get you thinking that there aren't other or better solutions to the problem.
The most important thing to do is to improve on your own understanding of the implementation. Right now, one of the best ways you can do that is to show us where you stand so we can help push you in the direction you need to go.
There is the problem, the prefab is creating in infinite times. I just want make for example 5 video objects at my parent transform.
Answer by gjf · Apr 13, 2016 at 06:01 PM
it's because you're creating them in Update()
- that runs EVERY frame... if you want them to be created at the start, put the code in Start()