Hi guys! I apologize if this questions have been already posted!.. But I guess I’m not good at searching!
Anyway, this is what I want to do: I created a GameObject array. I select the items of the array via Inspector. When my game is running, a GameObject (Character) is selected and it has to walk up to a target.position.
The problem I have, is that the character that have been already selected, is again selected! What I want to do is to avoid that! So, How can I select an item from an array just once? Please, C-Sharp!
Here is the code I have so far:
using UnityEngine;
using System.Collections;
public class RandomCharactersMovement : MonoBehaviour
{
public GameObject[] Characters = new GameObject[5];
public Vector3 Target = new Vector3(-18.03273f, 0.09936082f, 1.479195f);
float smooth = 3f;
public int TimeOut = 7;
public GameObject characterChosen;
// Use this for initialization
void Start ()
{
RandomCharacter ();
}
// Update is called once per frame
void Update ()
{
Invoke("MovingCharacter", TimeOut);
}
void MovingCharacter(){
if (characterChosen.transform.position.z <= 1.910322) {
characterChosen.transform.animation.CrossFade("walk");
characterChosen.transform.Translate(Vector3.forward * Time.deltaTime * smooth);
}
else {
characterChosen.transform.animation.CrossFade ("idle1");
Invoke("GoBack", 10);
}
}
void GoBack(){
characterChosen.transform.eulerAngles = new Vector3(0,180,0);
characterChosen.transform.animation.CrossFade ("walk");
characterChosen.transform.Translate (Vector3.forward * Time.deltaTime * smooth);
Invoke ("DestroyIt", 5);
}
void DestroyIt(){
GameObject.Destroy (characterChosen);
}
void RandomCharacter(){
int characterIndex = Random.Range (0, Characters.Length);
characterChosen = Characters[characterIndex];
}
}