Is it possible to detect a gameobject without GameObjectFind?

Hello, before starting the post, I wanted to say that I don’t speak English natively. So it will have grammatical errors lol.

Well, lately, I’m doing a project in Unity, which will serve as a learning experience, since I don’t have much experience in game development. In this project, I wanted to detect the data (mesh, etc.) of a game object without using gameobject find for this, as I saw that many programmers don’t like gameobject find. Would anyone know how this could be done?

In general, how you get a reference is usually quite contextual. Which is to say, it will depend on the situation.

In your instance, it looks like simply having one or more serialized fields, and assigning that via the inspector, should be sufficient. If you want the mesh of a game object, it’d make more sense to reference the MeshFilter component as opposed to the game object itself.

It would look like this roughly:

public class ExampleComponent : MonoBehaviour
{
	[SerializeField]
	private MeshFilter _meshFilter; // assign via the inspector
	
	private void Awake()
	{
		var mesh = _meshFilter.sharedMesh; // use shared mesh if you don't want the mesh to be instanced
		// do mesh things
	}
}
1 Like

thx, man (Arigato)

1 Like