Home>

I am doing chess and ran into a problem: I cannot get a component through a variable of typestring...

There are 6 classes: Pawn, King, etc. It is necessary to refer to this class through a variable, since each time the figure, and therefore the class, is different.

The entry below does not work

Canvas.GetComponent <
Global_Variables (). Active_Figure.GetComponent (Script) .Cancel ()

Writes:

Component does not contain a definition for Cancel .

Tell me what to do?

Can not understand anything. who are Pawn and King? what do they refer to? What does Canvas have to do with it? the figure is different every time and so what? write better in more detail, show the code. what and where are you trying to stick there. most likely you need to use polymorphism. but according to the chaotic description, there is little that can be answered

Алексей Шиманский2021-12-26 22:35:35

Read it again and you will understand. I make chess. I have an object, a cell, which accepts a figure selected by the player. The script that hangs on the figure depends on its name: the script Pawn hangs on the pawn, on the king King. In the cell script, I have to call certain functions of the shapes. This can be done through Canvas.GetComponent George Tuzikov2021-12-26 22:35:35

GetComponent implies accessing a component of a specific type. And you refer to an instance of which object /class and try to call a function there. If you need to call a function on a shape, and for each shape this function is the same, use it on shape classes -the parent class. If you only need the same name and number of function parameters, welcome to the interfaces. For example, 'interface iFigure {void func ();}' inherit from king classes, etc. Then getComponent () .func (); If it's completely stupid, use SendMessage (string nameFunc);

Xumera_hZ2021-12-26 22:35:35
  • Answer # 1

    As I wrote in the very first comment: use polymorphism, inheritance and other OOP delights.

    One of the options: create a base class for the shapes that will inherit from it. And then you can take the component like this:

    GetComponent <
    BASE_CLASS_NAME >
    ()
    

    to be more precise:

    Canvas.GetComponent <
    Global_Variables >
    () .Active_Figure.GetComponent <
    BASE_CLASS_NAME >
    

    and call the method.


    Test case.

    Parent of shapes:

    public abstract class FigureParent: MonoBehaviour {
        public virtual void Cancel () {
            Debug.Log ("cancel method from" + this);
        }
    }
    

    figure 1

    public class Pawn: FigureParent {
        public override void Cancel () {
            base.Cancel ();
            Debug.Log ("action from Pawn");
        }
    }
    

    figure 2

    public class King: FigureParent {
        public override void Cancel () {
            base.Cancel ();
            Debug.Log ("action from King");
        }
    }
    

    Next is a script, in which, presumably, two objects containing these scripts:

    public class TestFigure: MonoBehaviour {
        public GameObject fig1;
        public GameObject fig2;
        void Start () {
            fig1.GetComponent <
    FigureParent >
    () .Cancel ();
            fig2.GetComponent <
    FigureParent >
    () .Cancel ();
        }
    }
    

    This example will output to the console:

    cancel method from Pawn (Pawn)
    action from Pawn
    cancel method from King (King)
    action from King
    

    In general, you need to read about inheritance, polymorphism, abstract classes /methods, interfaces and everything with which you can combine classes with the same functionality and more ... In general, the basics :)

    Okay. I'll read it. Thank you)

    George Tuzikov2021-12-26 22:35:35

    @GeorgeTuzikov in general, not knowing exactly what you have written in the classes -either now inherit them from a common ancestor, and specify its name in GetComponent ... or make an interface in each class, implement its methods and specify the interface name in GetComponent. Should work without problem

    Алексей Шиманский2021-12-26 22:35:35

    Created the parent class Figure. Inherited the rest of the figures from him. All the same, GetComponent

    does not work -it does not see the Figure, although the Figure is public.

    George Tuzikov2021-12-26 22:35:35

    It is clear, I decided. Unity stumbled and did not see the parent class Figure-MonoBehaviour. Re-creating the script solved the problem. Everything is working. Thanks to all!

    George Tuzikov2021-12-26 22:35:35

    @GeorgeTuzikov didn't see the parent class Figure-MonoBehaviour -yeah. and I specially drew an example so as not to explain in words)) Well, if anything, you can put a daw) Do not forget to read books))

    Алексей Шиманский2021-12-26 22:35:35
  • Answer # 2

    As I wrote in the very first comment: use polymorphism, inheritance and other OOP delights.

    One of the options: create a base class for the shapes that will inherit from it. And then you can take the component like this:

    GetComponent <
    BASE_CLASS_NAME >
    ()
    

    to be more precise:

    Canvas.GetComponent <
    Global_Variables >
    () .Active_Figure.GetComponent <
    BASE_CLASS_NAME >
    

    and call the method.


    Test case.

    Parent of shapes:

    public abstract class FigureParent: MonoBehaviour {
        public virtual void Cancel () {
            Debug.Log ("cancel method from" + this);
        }
    }
    

    figure 1

    public class Pawn: FigureParent {
        public override void Cancel () {
            base.Cancel ();
            Debug.Log ("action from Pawn");
        }
    }
    

    figure 2

    public class King: FigureParent {
        public override void Cancel () {
            base.Cancel ();
            Debug.Log ("action from King");
        }
    }
    

    Next is a script, in which, presumably, two objects containing these scripts:

    public class TestFigure: MonoBehaviour {
        public GameObject fig1;
        public GameObject fig2;
        void Start () {
            fig1.GetComponent <
    FigureParent >
    () .Cancel ();
            fig2.GetComponent <
    FigureParent >
    () .Cancel ();
        }
    }
    

    This example will output to the console:

    cancel method from Pawn (Pawn)
    action from Pawn
    cancel method from King (King)
    action from King
    

    In general, you need to read about inheritance, polymorphism, abstract classes /methods, interfaces and everything with which you can combine classes with the same functionality and more ... In general, the basics :)

    Okay. I'll read it. Thank you)

    George Tuzikov2021-12-26 22:35:35

    @GeorgeTuzikov in general, not knowing exactly what you have written in the classes -either now inherit them from a common ancestor, and specify its name in GetComponent ... or make an interface in each class, implement its methods and specify the interface name in GetComponent. Should work without problem

    Алексей Шиманский2021-12-26 22:35:35

    Created the parent class Figure. Inherited the rest of the figures from him. All the same, GetComponent

    does not work -it does not see the Figure, although the Figure is public.

    George Tuzikov2021-12-26 22:35:35

    It is clear, I decided. Unity stumbled and did not see the parent class Figure-MonoBehaviour. Re-creating the script solved the problem. Everything is working. Thanks to all!

    George Tuzikov2021-12-26 22:35:35

    @GeorgeTuzikov didn't see the parent class Figure-MonoBehaviour -yeah. and I specially drew an example so as not to explain in words)) Well, if anything, you can put a daw) Do not forget to read books))

    Алексей Шиманский2021-12-26 22:35:35