0
\$\begingroup\$

This is my first time using interfaces so I imagine I'm doing something dumb. I have an interface called ILoadable inside ILoadable.cs;

using UnityEngine;
using System.Collections;

public interface ILoadable {
    void AssetLoader(); 
}

I also have a couple scripts that implements the interface, here's the small one;

using UnityEngine;
using System.Collections;

public class InputRebinder : MonoBehaviour, ILoadable {

    string upKey;



    public void AssetLoader(){
        upKey = ",";
    }
}

Then I have a script that I want to call all the instances of AssetLoader throughout my game.

using UnityEngine;
using System.Collections;

public class InterfaceControl : MonoBehaviour {
    ILoadable loader;
    // Use this for initialization
    void Awake () {
        AssetInterface();
    }

    // Update is called once per frame
    void Update () {

    }
    void AssetInterface(){

        loader.AssetLoader();
    }
}

I want all my assets to load up when the game starts. Assets in this case being links to gameobjects, so I'm essentially running a bunch of gameobject.find~ and I want to be able to summon those at will. Instead of working as advertised I'm getting this error;

NullReferenceException: Object reference not set to an instance of an object
InterfaceControl.AssetInterface () (at Assets/Resources/Scripts/Interfaces/InterfaceControl.cs:18)
InterfaceControl.Awake () (at Assets/Resources/Scripts/Interfaces/InterfaceControl.cs:9)

Which I know means I need to have an instance of loader in my interfacecontrol script. I think I could probably put in

loader = new InputRebinder(); But I don't want to just call input rebinder I want to call any script that has the ILoadable interface.

'for reference, I was following the instructions on dotnetperls.com in the interfaces section.

\$\endgroup\$
0
\$\begingroup\$

http://answers.unity3d.com/questions/46283/way-to-get-all-object-with-a-certain-componentscri.html

Here's what you're looking for. You want to find all components of this type in the scene. Since Unity 5 you should be able to find interface in the generic versions of Find* or Get* methods.

You should probably only do this in Awake, Start or some other method that does not run every frame since finding these can be pretty slow.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.