- Home /
multiple objects with same script, how to destroy one of them ?
this is the idea: there are many cubes with same script , this script destroys player and makes it respawn. i want to ask user to keep going if they watch a rewarded video so i made a panel and added button than wrote some code. problem is there are at least 90 cubes with same script so i cant really destroy the one player touched. these are my codes.
enemy script on cubes:
public class Carpisma : MonoBehaviour {
public GameObject patlama;
public GameObject PlayerDestroy;
public GameObject uiw;
private float t;
public int scene;
[SerializeField] private Transform player;
[SerializeField] private Transform respawnPoint;
public void ModeSelect()
{
StartCoroutine("Wait");
}
private void Start()
{
t = Time.time + 2;
}
void OnCollisionEnter(Collision obj)
{
Debug.Log(Time.time);
if (UnityEngine.Advertisements.Advertisement.IsReady() && Time.time>t)
{
t = Time.time + 2f;
Time.timeScale = 0f;
uiw.SetActive(!uiw.activeSelf);
}
else
{
Instantiate(patlama, this.gameObject.transform.position, this.gameObject.transform.rotation);
GameObject.Find("Sphere").SendMessage("Finish2");
GameObject.Destroy(PlayerDestroy);
Invoke("WaitingFunction", 1);
}
}
public void Watchit() {
UnityEngine.Advertisements.Advertisement.Show("rewardedVideo", new ShowOptions() { resultCallback = HandleAdResult });
}
public void HandleAdResult(ShowResult result)
{
switch (result)
{
case ShowResult.Finished:
Time.timeScale = 1f;
GameObject.Destroy(gameObject);
Debug.Log("bu itttt");
break;
case ShowResult.Skipped:
Instantiate(patlama, this.gameObject.transform.position, this.gameObject.transform.rotation);
GameObject.Find("Sphere").SendMessage("Finish2");
GameObject.Destroy(PlayerDestroy);
Invoke("WaitingFunction", 1);
break;
case ShowResult.Failed:
Instantiate(patlama, this.gameObject.transform.position, this.gameObject.transform.rotation);
GameObject.Find("Sphere").SendMessage("Finish2");
GameObject.Destroy(PlayerDestroy);
Invoke("WaitingFunction", 1);
break;
}
}
void WaitingFunction() {
SceneManager.LoadScene(scene);
}
public void again() {
Instantiate(patlama, this.gameObject.transform.position, this.gameObject.transform.rotation);
GameObject.Find("Sphere").SendMessage("Finish2");
GameObject.Destroy(PlayerDestroy);
Invoke("WaitingFunction", 1);
}
}
button script:
ublic void Watchit() {
Carpisma usit = FindObjectOfType<Carpisma>();
usit.Watchit();
ui.SetActive(!ui.activeSelf);
}
this is the last thing that i need to fix to able to publish it right after fixing this. please share your knowledge. I've been asking for this since yesterday :)
Answer by PolyvivalStudios · Feb 26, 2019 at 07:43 PM
So you basically want to destroy the cube that touched the player? Then it's simple. Just destroy the cube in the player killing function. Destroy(gameObject);