• 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 drodrii · Feb 28, 2014 at 08:13 PM · sprite

Create GameObject(Sprite) at Runtime

Hello everyone, so I'll cut to the chase. The idea for the script is the next:

 void Update
  {
    If(gameObjectCount < 5){
        //Create GameObject with certain properties.
     }
  }

Now I come to ask you people this because Im new to Unity and I'm getting the hang of it. 1.As for the logic, my concern is, where would I attach the script that manages the Game's Runtime? In this case the constant update on the scene. 2. As for the actual question, how do I manage to create the gameObject as child of another GameObject I've created?(For example an enemy that uses certain sprite Animations and certain script that makes it move?

I hope I don't take much of your time while reading/answering this questions.

have a great day!

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
Best Answer

Answer by DanielRS · Feb 28, 2014 at 08:31 PM

The usual approach to spawn Objects at runtime is using Instantiate, you can also save the instantiated object in a List, which gives you a reference to each object created and the amount of objects that has been instantiated. Like this:

 using UnityEngine;
 using System.Collections;
 using System.Collections.Generic;
 
 public class GameController : MonoBehaviour {
     public GameObject object2Spawn;
     private List<GameObject> spawnedObjects = new List<GameObject>();
     public int maxSpawns = 5;
     public float secondsBetweenSpawns = 5.0f;
     private float nextSpawnTime = 0;
 
     void Update()
     {
         if (Time.time > nextSpawnTime && spawnedObjects.Count< maxSpawns)
         {
             GameObject instantiatedObject = Instantiate(object2Spawn, transform.position, transform.rotation);
             spawnedObjects.Add(instantiatedObject);
             nextSpawnTime = Time.time + secondsBetweenSpawns;
         }    
     }
 
 }

Note that this code is not tested.

Comment
Add comment · Show 2 · 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 drodrii · Feb 28, 2014 at 08:42 PM 0
Share

@DanielRS Thanks for your answer it looks like this is what Im looking for, what would you attach it to?

avatar image DanielRS · Mar 01, 2014 at 01:20 AM 0
Share

@$$anonymous$$ii Any empty GameObject would work, what this script does is spawn "object2Spawn" at the position of the GameObject this script is attached to.

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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

21 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 avatar image avatar image avatar image avatar image avatar image

Related Questions

Implementing 2D Sprite Collisions 1 Answer

Animating 2D sprite upon movement script help. 2 Answers

How to scroll a sprite? 0 Answers

Personaje cambie de sprite al teleportarse [Characters change sprite to teleport] 0 Answers

runtime: script variable not processed as demanded 1 Answer

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges