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 vlamaster · Jul 25, 2014 at 09:35 PM · c#2dsprite

Spawning sprite in for loop

Hey guys I've just started using unity, and I have a question. I am using 2d.

I am trying to do this:

Using for loop spawn sprite(which is already added to the assets in Unity). I want to fill area with sprites. I know how to implement for loop in C#, but how do I spawn an existing sprite(not from path)?

Thanks in advice.

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 Saad_Khawaja · Jul 25, 2014 at 10:33 PM

You have to use GameObject.Instantiate to create a copy of a game object.

The best way is as follows:

  1. Drag the sprite onto the scene (make sure you are in 2D mode).

  2. A new gameobject will be created in the hierarchy window with the name of that sprite

  3. Create a new script and paste this in the code:

  4. Assign this script to any gameobject (not the sprite as it will become recursive)

  5. Drag the sprite gameobject to the spriteToDuplicate variable of the script in the inspector window

  6. Play and your sprite will be duplicated - and placed at x+=1 distance

    public GameObject spriteToDuplicate; void Start () {

                      Vector3 currentPosition = spriteToDuplicate.transform.position;
                         for(int i=0;i<10;i++)
                         {
                             GameObject tmpObj = GameObject.Instantiate(spriteToDuplicate,currentPosition,Quaternion.identity) as GameObject;
                             currentPosition += new Vector3(1f,0f,0f);
                         }
                     }
    
    
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 rahulpatil6220375 · Mar 23, 2019 at 04:37 AM 0
Share

how to set sprite images position ??

avatar image toddisarockstar rahulpatil6220375 · Mar 23, 2019 at 06:22 AM 0
Share

for a begginner you have to understand how unity works. everything is based on game objects. all game objects have components. components include your scripts. a sprite renderer is also considered a component to a gameobject. so what Saad_$$anonymous$$hawaja is suggesting is that you set up a game object with a sprite renderer component all set up. then duplicate the object with code. the answer to this question really has nothing to do with a sprite renderer. the appropriate answer duplicates anything reguardless of what components are attached google for: GameObject.Instantiate. it makes copies of anything in unity.

avatar image rahulpatil6220375 toddisarockstar · Mar 28, 2019 at 06:52 AM 0
Share

thanks for reply but set multiple objects and generate randomely like road? sometime object increment x direction and increment y direction

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Unity 2D all objects mash together! 0 Answers

How can I set the position and size of a sprite from inside a script ? 1 Answer

I want to make a certain sprite spawn somewhere periodically, how? 1 Answer

Sprite flip movement script not working! HELP! 0 Answers

Implementing 2D Sprite Collisions 1 Answer


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