Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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 BogdyHd · Mar 06, 2019 at 01:40 PM · movementmovement scriptobjectsmove an objectobject movement

How can I make a prefab object from a list to move?,How can I make an object from a list to move by itself?

So, I have a list of prefab objects and i randomly instantiate them at an fixed y axis position and i need them to move till -70. The problem is that I don't know which one will be spawned. Tried this:

 Enemies[Random.Range(0, 8)].transform.Translate(Vector3.forward * Time.deltaTime * -5f, Space.Self);

but dosen't work... This is my script so far:

 public class Enemy : MonoBehaviour
 {
     public float spawnRate = 1f;
 
     private float nextTimeToSpawn = 0f;
 
     public List<GameObject> Enemies;
 
 
 
 
     // Update is called once per frame
     void Update()
     {
         Enemies[Random.Range(0f, 8f)].transform.localScale = new Vector3(Random.Range(50f, 100f), Random.Range(50f, 100f), Random.Range(50f, 100f));
         Vector3 position = new Vector3(Random.Range(-8f, 8f), -20f, Random.Range(-4f, 5f));
         Enemies[Random.Range(0f, 8f)].transform.Translate(Vector3.forward * Time.deltaTime * -5f, Space.Self);
         if (Time.time >= nextTimeToSpawn)
         {
             Instantiate(Enemies[Random.Range(0f, 8f)], position, Quaternion.identity);
             nextTimeToSpawn = Time.time + 1f / spawnRate;
         }
         
     }
 }

,So, I have a list of objects, they are instantiated randomly at a fixed y axis position, and i want them to move on y axis by them-self. I tried this: Enemies[Random.Range(0, 8)].transform.Translate(Vector3.forward * Time.deltaTime * -5, Space.Self); Enemies is the list of prefab objects and because i don't really know which one is going to be instantiated I used Random.range but they are not moving.

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

2 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by Nivbot · Mar 06, 2019 at 01:55 PM

Put the movement script on the object you instantiate. Unless there is a good reason not to, all game objects should be controlled by scripts on themselves

Comment
Add comment · Show 1 · 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 BogdyHd · Mar 06, 2019 at 02:03 PM 0
Share

Thank you sir! I didn't realize that it could be so easy

avatar image
0

Answer by xxmariofer · Mar 06, 2019 at 02:03 PM

Enemies is a list of prefabs, not a list of instantiated objects, you need to store the instance of the object and move that instance something like this

 GameObject instanceClone;

  instanceClone = Instantiate(Enemies[Random.Range(0f, 8f)], position, Quaternion.identity);

and move the instanceClone .

Comment
Add comment · 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

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

159 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 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 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 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 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 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 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 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

How can i make it so that the object doesnt instantly go to top speed. Here is my code 1 Answer

Moving objects problem 1 Answer

How to drag a game object with a mouse (along x axis)? 1 Answer

How can I add a delay between moving objects ? 1 Answer

move enemy to old position of player 2 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