- Home /
Collision doesn't work with prefabs
Hi all, I'm working on a fishing game and I instantiated multiply fish with this script:
//
using System.Collections; // using System.Collections.Generic; // using UnityEngine;//
public class FishGenerator : MonoBehaviour { // public GameObject FishPrefab; // private double i = 0; // private double j = 0; //
void Update()
{
i = Time.realtimeSinceStartup; // tiene traccia del tempo reale
if ((i - j) > 3) // dopo 3 secondi inzia ad istanziare il prefab
{
var FishTransform = Instantiate(FishPrefab);
FishTransform.transform.position = new Vector3(12, Random.Range(-4, 4), -2);
j = i;
}
}
} I want that every fish instantiated collides with a Hoop, but only the first fish do it. The others (Clone) pass you by as if it were not there. How can I solve it? Do you have any suggestions? In below the Hoop script section for collision:
void OnTriggerEnter(Collider otherCollider) { Debug.Log(otherCollider.gameObject.name); audioSource.Play(); if(otherCollider.gameObject || otherCollider.gameObject.name == "Fish(Clone)") { Destroy(otherCollider.gameObject);
}
}
Thank you,Hi I'm working on a fishing game, and I want to generate multiply fish on the scene. I did this script to instantiate prefabs:
public class FishGenerator : MonoBehaviour { public GameObject FishPrefab; private double i = 0; private double j = 0;
void Update()
{
i = Time.realtimeSinceStartup;
if ((i - j) > 3)
{
var FishTransform = Instantiate(FishPrefab);
FishTransform.transform.position = new Vector3(12, Random.Range(-4, 4), -2);
j = i;
}
}
}
But only the first fish collide with the hoops. The others (Clone) pass you by as if it were not there. How can I solve it? Do you have any suggestions? In below the Hoop script section refers to the collision method:
void OnTriggerEnter(Collider otherCollider) { Debug.Log(otherCollider.gameObject.name); audioSource.Play(); if(otherCollider.gameObject || otherCollider.gameObject.name == "Fish(Clone)") { Destroy(otherCollider.gameObject);
}
}
Your answer
![](https://koobas.hobune.stream/wayback/20220612233408im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Change image visibility of newly instantiated prefab? 1 Answer
Update prefab instance with local values (Editor Script) 0 Answers
Making multiple instances of a prefab,Instantiating multiple instances of a prefab 1 Answer
Prefab trows Error: 0 Answers
Prefab disables a script out of runtime. 0 Answers