- Home /
Prefab instantiation: am I missing something?
bad englis incoming
Hi everyone. I'm sperimenting things in unity this time. I'm creating a 2d game where there are many casually generated shapes and similiars in the scene and you have to do things with 'em. To made the thing simple, i'm using prefabs of planes rotated in the right way with textures of what they represent. To renderize 'em I use this c# script, attached to the camera:
using UnityEngine;
using System;
using System.Collections;
using System.Collections.Generic;
public class Prova : MonoBehaviour
{
List<Transform> oggetti = new List<Transform>();
public Transform cerchio;
public Transform triangolo;
public int difficolta;
bool primoLoop;
public GameObject daRenderizzare = null;
// Use this for initialization
void Start()
{
difficolta = 2;
oggetti.Add(cerchio);
oggetti.Add(triangolo);
primoLoop = true;
}
// Update is called once per frame
void Update()
{
System.Random random = new System.Random();
List<GameObject> roba = new List<GameObject>();
int oggetto;
int oggettiInscena = random.Next(2, difficolta);
for (int i = 2; i < oggettiInscena; i++)
{
int xPos = random.Next(-160, 160);
int yPos = random.Next(-75, 60);
Vector3 posizioneOgg = new Vector3(xPos, yPos, 5);
oggetto = random.Next(0, difficolta);
daRenderizzare = (GameObject)Instantiate(oggetti[oggetto], posizioneOgg, Quaternion.identity);
}
//code here that doesn't let the system torecreate the shapes at every frame
primoLoop = false;
}
}
ignore all the variables written in italian: code is still code even written in japanese (nothing aganist nihonjin). Anyway what the code does (OR AT LEAST: what i'd want it to do but looks like i missed something) is that when i run the game, shapes comes out at random from the list and i can do what i want. But when i run the game, there is the background i pre-created but not the shapes. And that's not because they're not visible for a choordinate mistake because the inspector doesn't show 'em at all. The script doesn't contai errors. The prefabs, which i keep on a folder called Resources, contains the prefabs. what kind of prefab to use for cerchio and triangle is declared in the inspector of the camera (i drag the prefab into the desidered slot of the script dedicated session). So am i missing something (maybe i did some mistakes in the list declaration but i'm not sure (however i used simple arrays before for the same purpose and i had the same error))? How do i fix it?
Many thanks from now.
Regards, asduffo.
Answer by Travis-Bulford · Jul 01, 2013 at 03:20 PM
Well for one thing your for loop wont run. From what I can tell you will be generating a random number of 2.
This means you will never call the Instantiate code. That having been said I cant work out what you are trying to do exactly. Seams to me you want to build a lot of object very quickly. Remember Update will be called as often as Unity FPS. You might want to investigate FixedUpdate instead.
Also as a footnote I suggest using the Unity version of Random over the system one.
true. i fixed it, but that doesn't solve my problem anyway :(. What i want anyway is : renderize the shapes as i did in the code---> do something with inputs etc (which i omitted here in the comment at the end of update) ----> React (update score etc, omitted too) -----> return on the top of update. But it still doesn't work. And things like thread.sleep doesn't work either.
I am getting the idea you might be trying to actually do the draw calls when you mean to create a prefab.
Are you trying to write a render loop? Unity handles the rendering you only need to create the object and manage them. Not every frame create new ones.
I might be mistaken. Its a little difficult to make out your goals from the code.
well, i'm not very expert with unity ter$$anonymous$$ology (however i have a basiliar concept in $$anonymous$$d about what a draw call is), since i just started after a year of android 100% game program$$anonymous$$g with eclipse and traditional java code. Anyway what i want, basically, is to keep the sapes drawed until i want to (but before that, make 'em visible since it's my principal problem).
Your answer
Follow this Question
Related Questions
prefab is instantiating without a script 2 Answers
How to add an asset to a script-enabled public game object? 1 Answer
GameObject change Position after game started 1 Answer
PlayerRespawn class wont Instantiate the player prefab 1 Answer
Is it possible to disable/enable game-object inside Instantiate PreFab c# 0 Answers