• 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 /
  • Help Room /
avatar image
0
Question by bodionafun · Sep 19, 2020 at 06:18 PM · instantiate prefabclones

How can I have spawned gameObjects be treated as individuals by other scripts?

Hi, I am spawning objects onto a conveyor belt by adding them to a list onCollision and removing them on exit, only as soon as one exits all clones are removed from the list and the conveyor belt ceases moving them. I've given each a unique name but I'm not sure if this can be utilised. I just want the conveyor belt to keep moving the objects. My code is below;

Conveyor Script

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class ConveyorBelt : MonoBehaviour
 {
 public float ConveyorSpeed;
 public Vector3 ConveyorDirection;
 public List<GameObject> onBelt;
 
 void Start()
 {
 }
 
 void Update()
 {
     for(int i = 0; i <= onBelt.Count -1; i++)
     {
         onBelt[i].GetComponent<Rigidbody>().velocity = ConveyorSpeed * ConveyorDirection * Time.deltaTime;
     }
 }
 
 private void OnCollisionEnter(Collision collision)
 {
     onBelt.Add(collision.gameObject);
 }
 private void OnCollisionExit(Collision collision)
 {
     onBelt.Remove(collision.gameObject);
 }
 }

Spawner Script

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class Spawner : MonoBehaviour
 {
 
     public Transform spawnPos;
     public GameObject spawnee;
     public int nextNameNumber;
 
 
     
     void Update() {
 
 
         if(Input.GetKeyDown("p")) {
              
 
           Instantiate(spawnee, spawnPos.position, spawnPos.rotation);
           spawnee.name = "enemy"+(nextNameNumber);
           nextNameNumber ++;
     }
 }
 }

Oh and the destroy script

 using System.Collections;
 
 using System.Collections.Generic;
 
 using UnityEngine;
 
 
 public class Destroyer : MonoBehaviour {
     
 
 public float lifeTime = 10f;
 
 void Update () {
     if(lifeTime > 0) {
         lifeTime -= Time.deltaTime;
         if(lifeTime <= 0) {
             Destruction();
     }
 }
     if(this.transform.position.y <= -20) {
         Destruction();
     }
 }
 
 void OnCollisionEnter(Collision coll) {
     if(coll.gameObject.name == "Destroyer") {
         Destruction();
     }
 }
 
 void Destruction() {
     Destroy(this.gameObject);
     }
 }

Many thanks for your time and assistance. Sorry that the code is scrappy, I'm using notepad. Regards Bodi.

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

0 Replies

· Add your reply
  • Sort: 

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

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

Changing material dose not change the appearance of the game object 0 Answers

Clone of clones 2 Answers

Highlighting all clones instead of the one the mouse is over. 0 Answers

Add Clones of GameObject to List(array) 0 Answers

Instantiate And move the GameObject to the click position from another position. 1 Answer

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