1

I am making a game where the player first has to choose the type of control to use before playing. The three options being: Keyboard, Controller, Touch

The player must click the button corresponding to his choice. Each button runs this script when clicked on:

public class KeyboardButton : MonoBehaviour {

    public static int controller;

    public void buttonClick () {
        controller = 1;
    }
}

In reality, each button as its own script, where the value of controller is different depending on the script ran. The idea is that the value of this integer would be sent over to the script responsible of controlling the player so it will make use of the demanded input type. ie: if the keyboard button is selected, it will run the corresponding script, setting the integer value to 1. After the PlayerController script receives this value, it will know to only accept input from the keyboard.

I have consulted a lot of documentation, but a lot of it contains context-specific C# things that I don't understand and are irrelevant to what I want to do.

Also, I would not like an answer around the lines of: "You don't have to make the player choose a control type, here's how you can make your game accept all types of control at once." I already know all this stuff and there is a reason I want the player to make a choice. Furthermore, I would still like to know a way to transfer integers to be able to be more organized, rather than having a single script that does 90% of the things in the game.

1
  • If it is c# context specific that you dont understand how do you know it is irrelevant? Commented Mar 20, 2018 at 1:45

2 Answers 2

2

There are three way you can pass value to another script.

GetComponent

You can use GetComponent method to get another script.

public class KeyboardButton : MonoBehaviour {

    public int controller;

    //this is anotherScript instance
    public AnotherScript anotherScript;

    Start()
    {
        anotherScript = GameObject.Find("Name of Object").GetComponent<AnotherScript>();
    }


    public void buttonClick () {
        controller = 1;

        anotherScript.sendValue(controller); //send your value to another script
    }
}

Singleton

Let AnotherScript be a static Singleton,You can get the instance on other side.

public class AnotherScript : MonoBehaviour 
{
    //need to be static
    public static AnotherScript Current; 

    Start()
    {
        if(Current == null)
        {
            Current = new AnotherScript();
        }
    }


    public void sendValue(int val) 
    {
        //todo
    }
}

public class KeyboardButton : MonoBehaviour 
{

    public int controller;

    public void buttonClick () {
        controller = 1;


        AnotherScript.Current.sendValue(controller);//send your value to another script
    }
}

SendMessage

If you want to send a value to otherscript,SendMessage is a simple way you can choose.

ps:SendMessage method can just send a parameter.

public class KeyboardButton : MonoBehaviour 
{

    public void buttonClick () 
    {
        controller = 1;

        GameObject.Find("name of object").SendMessage("sendValue",controller);
    }
}
Sign up to request clarification or add additional context in comments.

2 Comments

you could also add, that since controller is a public static int that the other script can literally check its value by using KeyboardButton.controller
it's worth mentioning that the GameObject.Find function is not efficient performance wise so it's better to cache the object if you're planning on using it multiple times
0

As pointed out in one of the comments, you already exposed that value, you can refer to is via

Debug.Log(KeyboardButton.controller); 

without providing an instance. There's multiple other ways of doing it, as this way is only good to a certain level of complexity, after which it starts to get more muddy, but depending on what you need right know it might get you through. It is one of the valid ways, and probably the simplest one.

You may also want to know when the value has changed, for example you could use UntiyEvent and trigger it when value is changed

public class KeyboardButton : MonoBehaviour {
    public UnityEvent OnValueChanged;
    public static int controller;

    public void buttonClick () {
        controller = 1;
        OnValueChanged.Invoke();
    }
}

this is if you like to wire events in the editor. You could also do:

 public class KeyboardButton : MonoBehaviour {
        public static UnityEvent OnValueChanged;
        public static int controller;

        public void buttonClick () {
            controller = 1;
            OnValueChanged.Invoke();
        }
    }

the downside is that the event won't show up in the editor,but the upside is that you can set up a trigger without having to have a reference to the KeyboardButton instance that just got clicked.

public class ControllerChangeReactor : MonoBehaviour {
 void Start()
 {
   KeyboardButton.OnValueChanged.AddListener(React); // add event listener
 }

 void React()  // will get called when keyboard is clicked
 {
    Debug.Log(KeyboardButton.controller); 
 }


}

This approach can become limiting after you've written a dozen or so of those scripts, but a step up involves tailoring a custom system which is probably not worth it on your level (just yet). You can finish a simple game using the above approach, even if its not the most elegant.

You could also parametrize your script (expose 'WHAT DOES IT CHANGE' in editor), to avoid unnecessary multiplication of code

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.