• 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.

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

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

Trying to move an object between two points 2 Answers

I want to move the object more smooth way 1 Answer

GameObject won't move with transform.translate or rigidbody2d.moveposition,GameObject won't move in void Start() 1 Answer

Slowly move a GameObject on 1 axis, then destroy it. 1 Answer

How can i make diagonal movement? 1 Answer

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