简体   繁体   中英

How to access a method of a child class in c#?

I need to access essential parameter of gameObject.AddComponent() when it returns an instance of some class ...

The gameObject.AddComponent() function has return type Component .

The inheritance in the project looks this way:

Object <- Component <- Behaviour <- MonoBehaviour <- TDefault <- ...

Where A <- B means " B is inherited from A ".

I have no access to MonoBehaviour or its parents. They are provided by the Engine.

TDefault is the class from which I inherit all my classes but TEssential . It is written by me. It has essential property of type TEssential

Casting print(gameObject.AddComponent(effect_name) as TDefault); prints null .

print((gameObject.AddComponent(effect_name) as dynamic));

produces

Internal compiler error. See the console log for more information. output was:error CS0518: The predefined type System.Runtime.CompilerServices.CallSite is not defined or imported error CS0518: The predefined type System.Runtime.CompilerServices.CallSite 1' is not defined or imported

I tried adding

using System.Runtime.CompilerServices.CallSite;

but it only changes the error

Internal compiler error. See the console log for more information. output was:error CS0518: The predefined type System.Runtime.CompilerServices.CallSite' is not defined or imported error CS0518: The predefined type System.Runtime.CompilerServices.CallSite`1' is not defined or imported

I am sure that the returned variable has the type ... . The built-in editor shows it correctly.

How can I get the property?

Add component has a generic overload so you shoudl be able to do:

effect_name newComponent = gameObject.AddComponent<effect_name>();

after a little more research. are you sure that effect_name is inheriting TDefault ?

If you want to access to a method of a class (or component) attached to a gameobject (being a child or not), you can use gameObject.GetComponent<Myclass(or component)>().MyMethod([params]) .

If you want to access to a child of a game object -> gameobject.Transform.getChild(#) .

If you want to add a component then use the AddComponent method.

The solution is Casting to Interface:

1)

    using UnityEngine;
    using System.Collections;
    using NExtensionMethods;

    public class TDefault:MonoBehaviour,IDefault
    {
        public  TEssentail essential=new TEssentail();
        public TEssentail get_essential()
        {
            if(essential==null)
                essential=new TEssentail();
            return essential;
        }

        public TDefault ()
        {
        }
    }

2)

using UnityEngine;
using System.Collections;

public interface IDefault
{

    TEssentail get_essential();
}

3) TEssentail ess=(gameObject.AddComponent(effect_name) as IDefault).get_essential();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM