-1

I am working on a 2D Unity game where you can switch between different characters and moving them around when in control and while not in control they have their own behaviours. For example: I have two characters loli and the other is pop. The main character is loli and you can move her around as you please. But what if you need to move pop somewhere because there is an enemy that she isn't aware of. So, the player clicks on pop, moves her just like you could with loli and you can come back to controlling loli by clicking back to loli. In order to do that I'll need to have each character to read the player control script and follow what it says and they only read it if, loli or pop, was clicked by the player. Note that loli is the default character so when the game starts she's the one being controled by the player unless the player clicks on pop. The thing is I only know how to do this on C, python and lua, not C#. So how do I do that? Do I make a method in the player code and tell the others to reference it?

player's code

using System.Collections;
using System.Collections.Generic;
using UnityEngine;


public class user : MonoBehaviour
{
// Start is called before the first frame update
void Start()
{ 
    public int move = 5;
    private int acceleration = 2;

    public int Acceleration { get => acceleration; set => acceleration = value;
}


// Update is called once per frame
void Update()
{
    if(Input.GetKey(KeyCode.W))
    {
        transform.position = Vector2.up * move * Input.GetAxis("Vertical");
    }
    if (Input.GetKey(KeyCode.S))
    {
        transform.position = Vector2.down * move * Input.GetAxis("Vertical");
    }
    if (Input.GetKey(KeyCode.D))
    {
        transform.position = Vector2.right * move * Input.GetAxis("Horizontal");
    }
    if (Input.GetKey(KeyCode.A))
    {
        transform.position = Vector2.left * move * Input.GetAxis("Horizontal");
    }
}
}

Loli's code

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class loli : MonoBehaviour
{
// Start is called before the first frame update
void Start()
{

}

// Update is called once per frame
void Update()
{
    /*
     * read user.cs. until another character has been selected
     * then I'll do my own thing :D
     * Until I get clicked on D:
    */
}
}

pop's code:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class pop : MonoBehaviour
{
// Start is called before the first frame update
void Start()
{

}

// Update is called once per frame
void Update()
{
    /*
     * if i have been clicked on,
     * read user.cs.
     * else, do my own thing :D
    */
}
}
  • there are literally more than 100 ways to do this. but to make it habitable in Unity2D I'd suggest to attach `user` script to both `lolli` and `pop` game objects and use `GetComponent` in `lolli` and `pop` scripts to find the attached `user` script and to access its members and methods. – Bizhan Dec 28 '18 at 20:34
  • Well, there aren't 100 ways, but there are a few. [Here's a good starting point](https://stackoverflow.com/questions/4181668/execute-c-sharp-code-at-runtime-from-code-file). – Robert Harvey Dec 28 '18 at 20:36
  • @Bizhan, k I attached it then what now? – Alan Noobmaker Dec 28 '18 at 20:41
  • now write `GetComponent().MyMethod()` in `lolli` or `pop` to call `MyMethod` which you've supposedly previously defined in `user` – Bizhan Dec 28 '18 at 20:43

1 Answers1

0

What you are directly asking for is not an appropriate way to solve the problem, which is a question of how to share common behaviors between two GameObjects.

A better way to approach this is to put one instance of the user script on Loli's GameObject and a separate instance of it on Pop's GameObject. Then, in user, you add a boolean field that keeps track of if it is the currently active character:

public class user : MonoBehaviour
{
    public bool isActiveCharacter = false;

    ...

And in Update, don't do anything unnecessary if you're not the active character

public class user : MonoBehaviour
{

    ...

    void Update()
    {
        // Stuff that should happen on every frame, 
        // e.g., checking if this character is clicked on

        if (!isActiveCharacter) return;

        // Stuff that should only happen when character is selected 

        if(Input.GetKey(KeyCode.W))

        ...

And in the editor, you can set isActiveCharacter on Loli's user component to be true so that she is the default active character.

Then, you only need to write code that sets the appropriate isActiveCharacter when that character is clicked on, and sets the other one to false. One way is to give each user a reference to the other one (if there will only be two users):

public class user : MonoBehaviour
{
    public bool isActiveCharacter = false;
    public user other;

    ...

You would want to assign this reference in the editor (or use GameObject.Find to find it dynamically, which can be very slow). Then, in the code that detects clicks on the character, you set other.isActiveCharacter = false;.

Ruzihm
  • 19,749
  • 5
  • 36
  • 48