Say I have three different gameObjevcts, each representing rock, paper or scissors. These objects all have a Player script attached to them. The Player script has two functions i want to use, MoveSelected(), MoveNotSeleccted(). When the function CPUMove() is called, I want the program to randomly choose one of the three moves. My question is, how do I call a either of the functions fro another script RPS_Game? I thought of using Invoke() somehow but after trying couple of things with it, I got no where. Can someone please help me out?

public class RPS_Game {
    public void CPUMove() {
        int rand = Random.Range(1, 3);

        if(rand == 1) {
          //call MoveSelected() for one of the three Player objects and 
            MoveNotSelected() for the other two
        else if(rand == 2) {
          //call MoveSelected() for one of the three Player objects and 
            MoveNotSelected() for the other two
        else if(rand == 3) {
          //call MoveSelected() for one of the three Player objects and 
            MoveNotSelected() for the other two
  • \$\begingroup\$ Possible duplicate of How to call a method from another c# script using invoke? \$\endgroup\$ Feb 13 '18 at 5:11
  • \$\begingroup\$ I don't think this question duplicates that one, @SeyedMortezaKamaly. The linked question is specific to using the Invoke() method, which isn't mentioned here. One could certainly answer this one using Invoke(), but I don't think that would be the best answer to direct users to in this case. \$\endgroup\$
    – DMGregory
    Feb 13 '18 at 13:12
  • \$\begingroup\$ Ahmed, post your Player script, and a screenshot of your object hierarchy as well. These kinds of questions largely depend on the hierarchy of your objects in your project to determine how to make the calls. \$\endgroup\$
    – Stephan
    Feb 16 '18 at 19:31

You can just store a public reference to each of the scripts you want, and wire them up in the inspector:

public Player rockMove;
public Player paperMove;
public Player scissorsMove;

Then your script can say


Though how I'd do it would be more like this:

public class PlayerMove : MonoBehaviour {

     public enum MoveType : int {

     // This gives us a dropdown in the inspector 
     // to select a move type on each object.
     public MoveType moveType;

     // No need for two methods for on and off,
     // just pass a boolean representing true = selected
     // false = unselected (similar to Unity's SetActive method)
     public void SetMoveSelected(bool selected) {
        // ...

Then your script can get an array of PlayerMove components (or even search for them itself using FindObjectsOfType), and your CPUMove method can be very simple:

// Expose the array to populate or validate in the Inspector.
public PlayerMove[] moves;

public void CPUMove() {
    // Generate a random move 0, 1, or 2
    // (Random.Range on ints never selects the last item)
    // and cast it to a MoveType to get Rock, Paper, or Scissors.
    var selectedMoveType = (PlayerMove.MoveType)Random.Range(0, 3);

    // Iterate over the move objects and toggle them on/off
    // based on whether they match the randomly-selected move type.
    foreach(var move in playerMoves)
        move.SetMoveSelected(move.moveType == selectedMoveType);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.