I want to create a script where if the name of a gameObject is “Hi” and that gameObject goes through the -5 y axis it will be destroyed.
i’ve spent alot of time on this and i’ve actually tried. please unity explain
I want to create a script where if the name of a gameObject is “Hi” and that gameObject goes through the -5 y axis it will be destroyed.
i’ve spent alot of time on this and i’ve actually tried. please unity explain
More of curiosity than actually making something… Was using GameObject instead of Transform.
name of the object is a weird mode to search object. because the system add a (clone) when instantiate, and the system have to compare each gameobject.name
consuming a lot resources and maybe freezing the cpu
If u are new I suggest to use tag , and GameObject.FindGameObjectWithTag( "Hi")
or better, manage a dynamic array, can be a ArrayList
or List
and get access to all “Hi” objects instantly, however, if U know what are you doing here is a small script
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class FindHI : MonoBehaviour {
Transform[] AllHi;
bool SafeValve = false; //this is to stop overload of the cpu, if happens hold space
// Use this for initialization
/*void Start () {
}*/
// FixedUpdate is called each Time.fixedDeltaTime seconds
void FixedUpdate () {
//to test pupose, hold space to stop cpu overloading
if(Input.GetKey(KeyCode.Space))
SafeValve = true;
if (!SafeValve) {
//thats the first problem, this get all objects
AllHi = GameObject.FindObjectsOfType<Transform> ();
//thats the other problem, a loop iterates trough all objects on scene
foreach(Transform Hi in AllHi)
if (Hi.position.y<-5 && Hi.name == "Hi")
Destroy (Hi.gameObject);
}
}
}