Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by asduffo · Jul 01, 2013 at 03:13 PM · gameobjectinstantiateprefabloop

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.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0

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.

Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image asduffo · Jul 01, 2013 at 03:33 PM 0
Share

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.

avatar image Travis-Bulford · Jul 01, 2013 at 03:44 PM 0
Share

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.

avatar image asduffo · Jul 01, 2013 at 04:06 PM 0
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

16 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges