Z
Z
Zefirot2021-07-13 09:39:24
Unity
Zefirot, 2021-07-13 09:39:24

How to pass an object of any class to a method?

I have a method in each prefab with its own class that passes a click on it to the main class

public class Test1 : MonoBehaviour, IPointerClickHandler{
  public void OnPointerClick(PointerEventData pointerEventData){ GameController.instance.CellClick(this); }

.....
public class Test2 : MonoBehaviour, IPointerClickHandler{
  public void OnPointerClick(PointerEventData pointerEventData){ GameController.instance.CellClick(this); }

......
public class GameController : MonoBehaviour{
  public void CellClick(......){

How can I understand which object is being addressed and how can I manipulate it later?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
T
twobomb, 2021-07-13
@Zefirot

Well vrodeby all same prefabs are inherited from GameObject'a?
Can it be like this

public void CellClick(GameObject o){
   if(o.GetType() == typeof(Car)){
      Car car = (Car)o ;
   //todo
  }
   if(o.GetType() == typeof(Animal)){
      Animal animal= (Animal)o ;
    //todo
  }
}

If the manipulations are of the same type, then you can fasten the interfaces

G
GavriKos, 2021-07-13
@GavriKos

Why do you need to understand this? Make a common interface, for example, and that's it.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question