Search Unity

Creating Non-Static Instances Of A Rain AI Custom Action

Discussion in 'Scripting' started by Hypocrita_20XX, Aug 1, 2014.

  1. Hypocrita_20XX

    Hypocrita_20XX

    Joined:
    Feb 8, 2014
    Posts:
    27
    Hello and good day everyone!

    So, I have a script, called SwingSword, a custom Rain AI action, and I need to access some of its methods and such in a Monobehaviour script. I can do a static instance and it works just fine, however I need a non-static instance as this script is going to be attached to countless numbers of enemies (as far as I know, that's how it works, if I'm wrong, however, do let me know!)
    It's clear that the usual method of doing such a thing (gameObject.GetComponent<SwingSword>) is not going to work, as I get the following error.

    "The type 'SwingSword' cannot be used as type parameter 'T' in the generic type or method 'UnityEngine.GameObject.GetComponent<T>()'. There is no implicit reference conversion from 'SwingSword' to 'UnityEngine.Component'."

    I've also tried to add the component (gameObject.AddComponent,) but as the above error states, it appears to not be a component, so that didn't work.

    So I was wondering what I needed to do in order to get this to work? I'm sure it's possible, but I could be wrong.

    Thanks for your time and have a great day!
    -Hypocrita_20XX
     
  2. DoomSamurai

    DoomSamurai

    Joined:
    Oct 10, 2012
    Posts:
    159
    Check that your SwingSword script is inheriting from Monobehaviour.

    public class SwingSword: MonoBehaviour
    {
    //your code
    }


    If your SwingSword script is a MonoBehaviour, then you can attach it as a component on a GameObject and use the GetComponent<SwingSword>() method on that object.
     
  3. Hypocrita_20XX

    Hypocrita_20XX

    Joined:
    Feb 8, 2014
    Posts:
    27
    Hello DoomSamurai, thanks for your response!

    Unfortunately, this class doesn't inherit from Monobehaviour, it inherits from RAINAction, and after a little investigating, the script doesn't appear to be attached to anything at run-time.
     
  4. DoomSamurai

    DoomSamurai

    Joined:
    Oct 10, 2012
    Posts:
    159
    I'm not familiar with Rain AI. If it can't work as a MonoBehaviour, you usually need to have a way to keep references to relevant objects from the main object that uses them. This could be done by having, for example, a Character instantiate his own AI behaviours and keep them in a private variable. The Character could then call methods on those behaviour at any point in time.

    Another method would be to have a manager class that just keeps a list of objects. When an object "starts", it calls the instance of the manager and adds itself to the list. By using a dictionary instead of a list you can also fetch objects by a given key. This is best used for keeping track of objects that define the game such as Player objects or Monsters objects or whatever.

    From the documentation I found about Rain AI : "The Start method is always called before execution and is responsible for initialization."

    My guess is you should look into whether you can have the RainAction initialize and pass a reference to itself to the object that needs it.
     
  5. Hypocrita_20XX

    Hypocrita_20XX

    Joined:
    Feb 8, 2014
    Posts:
    27
    Hey DoomSamurai!

    As it so happens, the solution to this problem was quite simple, fortunately.
    For non-Monobehaviour scripts, it appears that all you have to do is something like the following.
    Code (CSharp):
    1. swingswordInstance = new SwingSword();
    There were other issues I ran into once I got that working, but it's all sorted out now and working beautifully.
    Thanks for taking the time to try to help me out, I really do appreciate it!

    Wishing you a great day!
    -Hypocrita_20XX
     
  6. RT_Jester

    RT_Jester

    Joined:
    Jul 21, 2011
    Posts:
    368
    Thanks for posting this @DoomSamurai and thanks for replying @Hypocrita_20XX ! If you guys have any more questions. comments, issues, please feel free to contact me (Rival Theory) directly! jester[at]rivaltheory[dot]com

    best,
    jester