- Home /
How make the same variable have different values for each object ?
I´m trying to make something that it is similar to a RTS game, when the player click on a object it becomes selected and his name get inside the variable selecionadoPlayer.
My problem is when i select a place for the selecionadoPlayer to go, all the objects (same prefab) go together to the location instead of just him
The solution that i found was write Selecionar.selecionadoPlayer.transform.position = Vector2.MoveTowards(Selecionar.selecionadoPlayer.transform.position, posicao, passo); but this do the object stop and the other object continue to the same place when the selected object change
MoverPlayerRTS.cs
public class MoverPlayerRTS : MonoBehaviour {
private Vector2 posicao;
public float speed;
void Start()
{
posicao = gameObject.transform.position;
}
void Update()
{
float passo = speed * Time.deltaTime;
if (Input.GetMouseButtonDown(1) && Selecionar.selecionadoPlayer != null)
{
posicao = new Vector2(Camera.main.ScreenToWorldPoint(Input.mousePosition).x, Camera.main.ScreenToWorldPoint(Input.mousePosition).y);
}
this.transform.position = Vector2.MoveTowards(this.transform.position, posicao, passo);
}
}
Selecionar.cs
public class Selecionar : MonoBehaviour
{
public static GameObject selecionadoPlayer;
public static GameObject selecionadoInimigo;
public Sprite normal;
public Sprite selecionado;
public static Sprite normalInimigo;
private SpriteRenderer sprender;
void OnMouseOver()
{
//Detectar objetos quando o mouse clicar em cima
//Convertendo a posição do mouse para um vetor 2d
if (Input.GetMouseButtonDown(0))
{
if (gameObject.tag == "Player")
{
//desseleciona inimigo
if (selecionadoInimigo != null)
{
sprender = selecionadoInimigo.GetComponent<SpriteRenderer>();
sprender.sprite = normalInimigo;
selecionadoInimigo = null;
}
//Seleciona e desseleciona o jogador
if (selecionadoPlayer == null)
{
selecionadoPlayer = gameObject;
}
//guarda posicao da ultima ordem
sprender = selecionadoPlayer.GetComponent<SpriteRenderer>();
sprender.sprite = normal;
sprender = gameObject.GetComponent<SpriteRenderer>();
sprender.sprite = selecionado;
selecionadoPlayer = gameObject;
Destruir.trava = false;
}
}
if (Input.GetMouseButtonDown(1))
{
if (gameObject.tag == "Celula" && selecionadoPlayer != null)
{
//Seleciona e desseleciona o objeto
if (selecionadoInimigo == null)
{
selecionadoInimigo = gameObject;
}
sprender = selecionadoInimigo.GetComponent<SpriteRenderer>();
sprender.sprite = normal;
normalInimigo = normal;
sprender = gameObject.GetComponent<SpriteRenderer>();
sprender.sprite = selecionado;
selecionadoInimigo = gameObject;
}
}
}
}
Answer by TheKnightsofUnity · Jul 06, 2018 at 02:57 PM
Hey there @Fellimao !
When you set a variable as 'static' as you have in Selectionar.cs it basically means that all the instances of this class will share this variable among themselves. So what you want to do here is to remove the 'static' keyword from these variable declarations:
public GameObject selecionadoPlayer;
public GameObject selecionadoInimigo;
Hope that helps, cheers!