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

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

rigidbody,addforce is affecting all clones which are clicked on through raycast 0 Answers

Instantiated prefabs all stacking on top of each other 1 Answer

Instantiate and launch prefab that's overlapping player? 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