• 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 /
This question was closed Nov 09, 2014 at 05:26 PM by robertbu for the following reason:

Multiple technical questions, lack of context

avatar image
Question by ivikd · Nov 09, 2014 at 05:22 PM · instantiaterandomspawn

How to generate random object with attributes?

Hello all.

I'm working on a 2D game where i need a new object to be generated every few seconds somewhere in the screen. This object can be one of 20 items, divided into 4 categories (cat1, cat2, cat3, cat4). Each category has 5 different images to pick from, but technically all objects in cat1, for example, behave identically except for the image file attached to them. I've read that I can use an Instantiate method to spawn my objects, but I'm not sure how I would go about setting it up to pick a category and an image from it.

So far, I've managed to instantiate my object in a random position (restricted a bit):

  var myObject : Transform;
  function Update()
  {
      Instantiate(myObject,new Vector3(UnityEngine.Random.Range(-3f,3f),UnityEngine.Random.Range(-3f,3f),UnityEngine.Random.Range(-3f,3f)), Quaternion.identity);
  }

However, this is using the gameObject with a preset image for its sprite via the Sprite Renderer component. Ideally, I'd want it to randomly select an image from my assets folder.

Any help would be much appreciated!

Comment

People who like this

0 Show 1
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 robertbu · Nov 09, 2014 at 05:27 PM 0
Share

There are multiple technical questions here, and there is not enough context to give you an accurate answer to the most obvious one...how to change a texture of a game object. Unity Answers addresses a single, specific technical question.

To start, research the many ways the texture can be change. Do some goggling on the follow:

  • Material.mainTexture

  • sprite sheet

  • texture atlas

  • uv coordinates

  • material.mainTextureOffset and Material.mainTextureScale

  • texture array

  • Resources.Load() and Resources.LoadAll()

  • Sprite

0 Replies

  • Sort: 

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

2 People are following this question.

avatar image avatar image

Related Questions

Spawning different random objects at the same position? 2 Answers

An algorithm to automatically instantiate objects 2 Answers

Spawn at random? 1 Answer

Spawn enemies so they aren't spawned on top of each other (C#) 1 Answer

i need Spawn Object loop if find object tag ("Clone") in list ,when i delete find object tag ,cript is good but no find object, please help me 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