Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

C# Question about referencing other scripts

Discussion in 'Editor & General Support' started by ximitimix, Sep 10, 2014.

  1. ximitimix

    ximitimix

    Joined:
    Jan 29, 2014
    Posts:
    19
    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
    Code (CSharp):
    1.  
    2. [RequireComponent(typeof(Menusys))]
    3. public class TouchLogic : MonoBehaviour {
    4.     private Menusys controller;
    5.  
    6. void Awake(){
    7.  
    8.         controller = GetComponent<Menusys>();
    9. }
    10.  
    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
     
  2. TheSin

    TheSin

    Joined:
    Aug 4, 2011
    Posts:
    150
    Google unity singletons.
     
  3. ximitimix

    ximitimix

    Joined:
    Jan 29, 2014
    Posts:
    19
    that seems like a lot for what i need, is there no way to simply replace this
    Code (CSharp):
    1. private Menusys controller;
    with something like this
    Code (CSharp):
    1. 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
     
    Chambers likes this.
  4. Chambers

    Chambers

    Joined:
    Apr 4, 2011
    Posts:
    70
    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?
     
  5. flaminghairball

    flaminghairball

    Joined:
    Jun 12, 2008
    Posts:
    868
    Code (csharp):
    1. public Menusys controller;
     
  6. ximitimix

    ximitimix

    Joined:
    Jan 29, 2014
    Posts:
    19
    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
     
  7. flaminghairball

    flaminghairball

    Joined:
    Jun 12, 2008
    Posts:
    868
    Code (csharp):
    1.  
    2. public MonoBehaviour controller;
    3.  
    Will allow you to drop any MonoBehaviour into the slot (which is what most scripts inherit from).
     
  8. ximitimix

    ximitimix

    Joined:
    Jan 29, 2014
    Posts:
    19
    im at work right now, but im going to try this when i get home, appears to be exactly what i was looking for!
    Code (CSharp):
    1. public MonoBehaviour controller;
    thanks flaminghairball
     
  9. TheSin

    TheSin

    Joined:
    Aug 4, 2011
    Posts:
    150
    Yeah, I think I glanced through the question . Sorry.