- Home /
Deleting a specific name if it passes through the -5 y axis
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
Do all of the 'Hi' gameObjects already have their own script attached to them?
Answer by DCordoba · Jan 13, 2019 at 09:04 AM
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);
}
}
}
Why FixedUpdate? The input will not always be detected if the code isn’t running at every frame.
its teorically true, interesting, it must not detect it, but in fact it does, even with low framerate, idk the reason, but, in case of fail, I put "hold" there, so I hope it will catch the imput sometimes.
After do the script I put the safe, in the script without the safe the election was because the delete is based on movement. I guess he need to use it on objects moved by physics/ transforms with deltatime correction, because of that, is better to delete it on fixeddeltatime... other solution is put a massive collider on the -5, still will consume less resources... well, depend the amount of transforms on scene... and collisions per second...
It’s not theoretical. There’s going to be times when the input is not going to be detected. I’ve had this problem before, and so have many other people (you can google it if you don’t believe me). Putting input in FixedUpdate will occasionally lead to input loss.
DO NOT DO any FindObjects or GetComponent in update functions!
EVER!
It's slow as hell and stupid as hell! Either find them all in Awake/Start, or if they are created at runtime, make them register themselves to the script which handles their destroying
Answer by butigirilla · Jan 13, 2019 at 06:16 PM
More of curiosity than actually making something... Was using GameObject instead of Transform.