GetScript() error

when i use GetScript,its error; which problem it is?



message [Info] E:\flax\bat\Source\Game\AgentSystem\AgentHitBox.cs(14,34,14,40): error CS0119: ‘IAgent’ is a type, which is not valid in the given context

GetScript can be used in two different ways, either by giving it a type parameter or the type as a normal parameter. You need to do either GetScript<IAgent>() or GetScript(typeof(IAgent)).

1 Like

thanks,I will try.

Hello friends…

Take a step back and look at the variable named agent in OnStart.
What is the value of agent when used here?
As written, agent is not initialized yet when used.

The quick fix is

public class AgentHitBox : Script
    {
        Actor agent; // Actor here is type
        IAgent target;

        public override void OnStart()
        {
            agent = Actor; // Actor here is game instance of attached not type
            // target = agent.GetScript<IAgent>(); // whoops...todo more discussion here 
        }

        .
        .
        .
    }

Accessing scene objects | Flax Documentation (flaxengine.com)
Class Actor | Flax Documentation (flaxengine.com)
Debug Log | Flax Documentation (flaxengine.com)

I know,I set it in editor.

Okay…You set an instance of type Actor in the editor to agent. Cool, my bad.
Back to the error, we’re setting the agent.GetScript argument as an abstract class type.
You wouldn’t be able to attach the IAgent.cs script to an editor actor either.
Implement your abstact class and just use it.

    public class myAgent : IAgent
    {
        public override void GetDamage(float damage, Vector3 pos)
        {
            health -= damage;
        }
    }


    public class Trap : Script
    {
        public Actor actor; // Must be set in editor
        IAgent target;

        public override void OnStart()
        {
            target = new myAgent();
        }

        public override void OnFixedUpdate()
        {
            // todo : game logic/conditional
            target.GetDamage(4.0f, Vector3.Zero);
        }
    }

thanks, I will try it in my project.