Best approach for swapping sprites and stats between two objects?

Hi all,

I have two 2D game objects, Thrower and Catcher, with their own sprites and different values for the same set of stats (speed, stamina,…).

Say object 1 starts as Thrower and object 2 as Catcher. Then on mouse click, Thrower launches a ball (2D sprite object) to Catcher.

When Catcher gets the ball they swap sprites and stats: object 1 gets the Catcher’s sprite and stats, and objects 2 gets the Thrower’s sprite and stats.

What is the best approach to swap those stats and sprites?

Should I have two separate objects ore two instances of the same objects?

Should I use classes and inheritance?

Should I use tags on the two objects and load sprites and stats based on the current object tag?

Any directional help is greatly appreciated!

It’s obviously a matter of taste how you do this, but for inspiration’s sake I’ll just briefly outline how I’d do it.

I’d have an enum that was the state of each of these thrower/catchers … I’ll just call them Actors.

public enum EActorState
{
  Catcher,
  Thrower,
}

Then I’d make a private var of type EActorState in the Actor class.

private EActorState actorState;

and use a property to access it from outside.

public EActorState ActorState
{
  get
  {
    return this.actorState;
  }
  set
  {
    if(this.actorState != value)
    {
      //Something changed, do all the switching-of-sprites and such here
      this.actorState = value;
    }
  }
}

Some piece of code would need to set the state of the actors when the ball is thrown of course.

Anyhow, that’s just one approach. I like it because as long as you change stuff from the outside, you can count on the Actor being updated automatically. Oh, and can’t compile just now, so please bear with me if it has a pile of typos.

@NerdClown, I’m trying to implement your solution (and learning about accessors and enums) but I’m stuck in two places:

  • in script 1: the logic for swapping stats (or sprites) is not working
  • in script 2: I can’t set the property to change from Thrower to Catcher and from Catcher to Thrower on both players

Player1 and player2 have this script 1 attached

using UnityEngine;
using System.Collections;

public enum EPlayerState {
	Thrower,
	Catcher
}

public class playerManager : MonoBehaviour {

	private EPlayerState playerState;
	public EPlayerState PlayerState {
		
		get { 
			return this.playerState;
		}

		set { 
			if (this.playerState != value) {
			
				switch(this.playerState) {

				case EPlayerState.Thrower:

					health = 10;
					speed = 100;
					//Debug.Log ("Thrower");
					break;

				case EPlayerState.Catcher:

					health = 20;
					speed = 200;
					//Debug.Log ("Catcher");
					break;
				}

				this.playerState = value;
			}
		}
	}
	public int health;
	public int speed;
}

The ball has script 2 attached:

using UnityEngine;
using System.Collections;

public class swapPlayers : MonoBehaviour {

	EPlayerState newPlayerState = new EPlayerState ();

	private GameObject catcher;
	private GameObject thrower;

	void Start() {
		catcher = GameObject.FindWithTag("catcher");
		thrower = GameObject.FindWithTag("thrower");
	}

	void OnTriggerEnter2D(Collider2D other) {

	if (other.CompareTag("catcher")) {
		
			//set enum to "thrower" on other
			newPlayerState = EPlayerState.Thrower;

			//set enum to "catcher" on thrower
			}
		}
	}