Simple interfaces question (Check if a class inherits an interface)

Hey everyone, I’m programming an A.R game for mobile and when the tracking is lost I need to disable some mono behaviours. But, in some scripts the void OnEnable and OnDisable should be only called in some special ocasions, so I create an interface and in these scripts I inherit the interface which contains a property called isDisabledDueToTracking.

This way I can know if the behaviour has been disabled because the special ocasion was reached or because the tracking was lost. Until now, everything is ok, but I’m having problems to set values to this property. Here’s the code:

public interface IMonoSpecial {
    	
    	bool isDisabledDueToTracking {
    		
    		get;
    		set;
    		
    	}
    	
    }

    using UnityEngine;
    using System.Collections.Generic;
    
    
    public class gameHandler : MonoBehaviour {
    	
    	/// [.......]
    	
    	public void switchMonoState (bool state) {
    		
    		if(!state) {
    		
    		MonoBehaviour[] mono = (MonoBehaviour[])GameObject.FindObjectsOfType(typeof(MonoBehaviour));
    		
    			foreach(MonoBehaviour _mono in mono) {
    			
    				if(_mono != this && _mono.gameObject != ARCamera && _mono.gameObject != ImageTarget && _mono != _trackerHandler) {
    		
    				_mono.enabled = state;
    					
    				if(_mono is IMonoSpecial) _mono.isDisabledDueToTracking = true; 

// THE PROBLEM IS IN THE LINE ABOVE LINE: Type `UnityEngine.MonoBehaviour' does not contain a definition for `IMonoSpecial' and no extension method `IMonoSpecial' of type `UnityEngine.MonoBehaviour' could be found
    					
    				activedBehaviours.Add(_mono);
    					
    					}
    				
    				}
    			
    			} else {
    		
    		foreach(MonoBehaviour _mono in activedBehaviours) {
    			
    			_mono.enabled = true;
    			
    			}
    			
    			activedBehaviours = new List<MonoBehaviour>();
    			
    	}
    }
    
    // [.............]
    
    }

Anyone can help me with this? I’m not sure about what I’m missing in the code. Interfaces are not my strong. heheh

Thanks from now!!!

Ok so your problem in that line is that while you are doing an “is” correctly you would also need an “as” in the subsequent part to make the cast - but don’t run off and do that yet :slight_smile:

You won’t be finding many gameobjects anyway, because you need all of the components that implement the interface not the game objects.

So:

  using System.Linq;  

  ...

   foreach(var _mono in GameObject.FindObjectsOfType(typeof(GameObject)) //Get all game objects
                             .Cast<GameObject>() //Cast the results to GameObject
                             .Where(g=>g != ARCamera && g != ImageTarget) //Game object conditions
                             .SelectMany(g=>g.GetComponents<MonoBehaviour>()) //Get the components have to use the non-generic version to get interfaces
                             .Where(i=>i != this && i != _trackerHandler) //Behaviour conditions
                ) {
           _mono.enabled = state;
           if(_mono is IMonoSpecial)
                 (_mono as IMonoSpecial).isDisabledDueToTracking = true;
           activedBehaviours.Add(_mono);
        }