C# Question about referencing other scripts

ok so i have a script for touch input that requires a controller script and calls OnTouchStart() and OnTouchEnd(), I have this working just fine and it looks like this

[RequireComponent(typeof(Menusys))]
public class TouchLogic : MonoBehaviour {
    private Menusys controller;

void Awake(){

        controller = GetComponent<Menusys>();
}

then in the update i call “controller.OnTouchStart()” ect… what i want to do is not limit this to a single script and make this controller a public variable so i can just drop a script in using the inspector

Google unity singletons.

that seems like a lot for what i need, is there no way to simply replace this private Menusys controller; with something like this public script controller; i know that doesn’t work, but is there a type that does work for just any script? i would like to write different controllers for different scenes and just drop in the one i need. if using singletons is the only way, i might have to try something different

1 Like

I think you could avoid using Singletons here by doing something like you posted above with an exposed variable or method, you might need to refactor some of your current code to accommodate the change between public and private. Its quite difficult to visualize the script structure (at least I’m finding it difficult) with the limited code you’ve shown but I definitely think you would be able to do this without using Singletons.

What error does changing the code to public and dropping a different script in the Inspector give exactly?

public Menusys controller;

if i try dropping in a different script it complains about not finding Menusys. what i had was a touch controller script that passed messages to a game controller, one of those controllers was for my menu system aka(Menusys), and one is for in game. i solved this anyway and just wrote a separate touch script for ingame, i hate having duplicate code, but this is fine for now

public MonoBehaviour controller;

Will allow you to drop any MonoBehaviour into the slot (which is what most scripts inherit from).

im at work right now, but im going to try this when i get home, appears to be exactly what i was looking for!

public MonoBehaviour controller;

thanks flaminghairball

Yeah, I think I glanced through the question . Sorry.