Answer the question
In order to leave comments, you need to log in
What are the ways to interact between C# Unity classes/objects?
I know about the "GetComponent" methods, about the method of passing objects to public fields or fields with the "SerializeField" attribute through the inspector. What I would like to know, there is a class that is attached to one of the objects on the stage, there are many other objects on the stage, how can I access any of the objects on the stage through that class? In general, what are the ways?
Answer the question
In order to leave comments, you need to log in
Why do you need to access any object on the stage from the class? This question immediately seems incorrect, because I can hardly imagine such situations when ONE class should have a connection with ALL objects. Are these objects ALL of the same type?
I can see two ways offhand.
1. If you need the same code to work for several game objects, for example, when they appear on the stage (these can be different moments), then you can hang the same squeaks on all the necessary GOs. The script will contact the GO on which it hangs and will change or control something there. Most often, it is something behavioral. Mob behavior, effect, etc.
2. You will have a controller with a list of objects that it controls. And the controller, receiving information from the system, will decide which object and what should be done ("kill", pause, spawn something, etc.).
As an addition. If you need a certain controller that should be available from different parts of the project, then it is made a singleton, as an option. This controller is busy with important things, such as controlling the state of the game (playing, pause, starting, etc.) and toggling it. And other scripts can clarify with him, but what do we have there in general? Pause? Ah, well then I won't be spawning my effects/doing other local stuff.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question