Advice on types in c#

Hello!

I was wondering if there is a better way to deal with controller scripts when you have more than one type … hmm, let me show some code…

		if(isDrive){	
			gameDriveController = FindObjectOfType(typeof(DriveWorldController)) as DriveWorldController;
		} else {
			gameController = FindObjectOfType(typeof(GameController)) as GameController;
		}

So here, I’ve got two game controllers. One is for driving and the other is for flying, which are in seperate scenes. From my UI script I need to contact the game controller and I use the same UI script in both sections of the game so I need to decide what type of game it is, then pick the appropriate gamecontroller.

How would you guys deal with this? Is there a cleaner way to do it without having to have two references like this?

Thanks for any ideas - I just hate the idea that I’m missing some neat way of doing it! :slight_smile:

You could make a “Controller” class that implements the interface that is shared between your two controller classes and then have your two specific classes inherit from Controller. Inheritance:

Alternatively, if your two classes only share a few variables or functions, you could define an interface:
http://msdn.microsoft.com/en-us/library/87d83y5b(VS.80).aspx

Thanks, Tom.

I was looking in this direction, but wasn’t sure what would be the best solution for this type of problem.

Following your advice, I am going to go ahead and build a shared interface that each individual controller could inherit.

:slight_smile: