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.
Your answer

Follow this Question
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