j'ai tenter plusieurs variante mais j'ai toujours la même erreur.
La partie de mon script qui me donne l'erreur, devrais me donnais la cible avec le tag "Ressources" la plus proche de l'unité avec ce script.
Code : Tout sélectionner
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.AI;
public class Unites : MonoBehaviour {
NavMeshAgent agent;
public bool selection;
public bool testselect;
public GameObject selectionObjet;
public GameObject deplacementObjet;
public float TimeDeplacement;
public int UniteN;
public int UniteNcalcul;
public int posz;
public bool Ouvrier;
public bool mine;
public bool ActiveMine;
public float TempsRecolte;
public GameObject MineOgjet;
public bool transporteur;
public bool transporte;
public bool ActionPrioritaire;
public Vector3 PosDestination;
public GameObject Charbon;
public GameObject Fer;
public GameObject Aluminium;
public GameObject Cuivre;
public GameObject Or;
public GameObject Diamant;
public GameObject Entrepot;
public int NList;
public int ListN;
public Vector3 Target;
public bool recherche;
public bool destination;
public float RDistance;
RaycastHit hit;
void Start () {
TimeDeplacement = 2;
agent = GetComponent<NavMeshAgent> ();
selection = false;
testselect = false;
ActionPrioritaire = false;
recherche = true;
destination = false;
}
void Update () {
if (mine == true && ActiveMine == true){ //faire apparaitre une ressource devant la mine
TempsRecolte = TempsRecolte + 1 * Time.deltaTime;
if (TempsRecolte > 10 /* - amelioration*/) {
TempsRecolte = 0;
Mine.Ressources = Mine.Ressources + 1;
}
}
TimeDeplacement = TimeDeplacement + 1 * Time.deltaTime;
if (TimeDeplacement < 1f) { // temps d'affichage du cercle blanc ( destination)
deplacementObjet.SetActive (true);
deplacementObjet.transform.SetPositionAndRotation (new Vector3 (agent.destination.x, 0.1f, agent.destination.z), Quaternion.identity);
PosDestination = deplacementObjet.transform.position;
} else {
deplacementObjet.SetActive (false);
}
if (selection) {
if (testselect == false) {
testselect = true;
Select.NUnitéSelec = Select.NUnitéSelec + 1;
UniteN = Select.NUnitéSelec;
}
selectionObjet.SetActive (true);
} else {
if (testselect == true) {
testselect = false;
Select.NUnitéSelec = Select.NUnitéSelec - 1;
}
selectionObjet.SetActive (false);
}
if (Input.GetMouseButtonDown (0)) {
if (Input.GetKey (KeyCode.LeftControl)) {
} else {
selection = false;
}
}
if (selection) {
if (Input.GetMouseButtonDown (1)) { // deplacel'unite a l'endroit cible
if (Physics.Raycast (Camera.main.ScreenPointToRay (Input.mousePosition), out hit, 200)) {
TimeDeplacement = 0;
Deplacement ();
UniteNcalcul = UniteN;
posz = 0;
ActionPrioritaire = true;
}
}
if (Ouvrier == true){ // Unité Ouvrier
if (hit.collider.name == "Mine") {
mine = true;
MineOgjet = hit.collider.gameObject;
} else {
mine = false;
TempsRecolte = 0;
}
}
}
if (Vector3.Distance (gameObject.transform.position, PosDestination) < 1f) { // Unité et arrivé à destination (mouvement indiqué par le joueur)
ActionPrioritaire = false;
}
if (transporteur == true) { // Unité Transporteur
if (ActionPrioritaire == false) {
if ( transporte == false){
if (GameObject.FindWithTag ("Ressources") != null) {
if (destination == true) {
agent.destination = Target;
NList = ListN;
} else if (recherche == true) {
ListN = 0;
RDistance = 1;
recherche = false;
RTarget ();
}
}
}else {
destination = false;
recherche = true;
agent.destination = Entrepot.transform.position;
}
}
}
}
void OnTriggerEnter (Collider other){
if (other.gameObject.name == "Selection") {
selection = true;
}
if (other.gameObject.name == "Mine") {
ActiveMine = true;
}
if (transporteur == true) {
if (transporte == false) { // le transporteur prend une ressource
if (other.gameObject.name == "Charbon") {
Destroy (other.gameObject);
Instantiate (Charbon, gameObject.transform).name = "Charbon1";
transporte = true;
Interface.ListRessources.RemoveAt (NList);
} else if (other.gameObject.name == "Fer") {
Destroy (other.gameObject);
Instantiate (Fer, gameObject.transform).name = "Fer1";
transporte = true;
Interface.ListRessources.RemoveAt (NList);
} else if (other.gameObject.name == "Aluminium") {
Destroy (other.gameObject);
Instantiate (Aluminium, gameObject.transform).name = "Aluminium1";
transporte = true;
Interface.ListRessources.RemoveAt (NList);
} else if (other.gameObject.name == "Cuivre") {
Destroy (other.gameObject);
Instantiate (Cuivre, gameObject.transform).name = "Cuivre1";
transporte = true;
Interface.ListRessources.RemoveAt (NList);
} else if (other.gameObject.name == "Or") {
Destroy (other.gameObject);
Instantiate (Or, gameObject.transform).name = "Or1";
transporte = true;
Interface.ListRessources.RemoveAt (NList);
} else if (other.gameObject.name == "Diamant") {
Destroy (other.gameObject);
Instantiate (Diamant, gameObject.transform).name = "Diamant1";
transporte = true;
Interface.ListRessources.RemoveAt (NList);
}
} else if (other.gameObject.name == "Entrepot"){ // Le transporteur dépose la ressource
if (transform.GetChild (2).name == "Charbon1") {
transform.GetChild (2).name = "1";
Interface.Charbon = Interface.Charbon + 1;
transporte = false;
} else if (transform.GetChild (2).name == "Fer1") {
transform.GetChild (2).name = "1";
Interface.fer = Interface.fer + 1;
transporte = false;
} else if (transform.GetChild (2).name == "Aluminium1") {
transform.GetChild (2).name = "1";
Interface.Aluminium = Interface.Aluminium + 1;
transporte = false;
} else if (transform.GetChild (2).name == "Cuivre1") {
transform.GetChild (2).name = "1";
Interface.Cuivre = Interface.Cuivre + 1;
transporte = false;
} else if (transform.GetChild (2).name == "Or1") {
transform.GetChild (2).name = "1";
Interface.Or = Interface.Or + 1;
transporte = false;
} else if (transform.GetChild (2).name == "Diamant1") {
transform.GetChild (2).name = "1";
Interface.Diamant = Interface.Diamant + 1;
transporte = false;
}
}
}
}
void OnTriggerExit (Collider other){
if (other.gameObject.name == "Selection") {
selection = false;
}
if (other.gameObject.name == "Mine") {
ActiveMine = false;
}
}
void RTarget () { // Calcul pour connaitre la cible (ressources la plus proches)
Debug.Log ("RTarget () _ recherche : " + recherche + " | destination : " + destination + " | ListN : " + ListN + " | RDistance : " + RDistance + " | " + Interface.ListRessources.Count);
if (Vector3.Distance (gameObject.transform.position, Interface.ListRessources [ListN].transform.position) < RDistance) {
Target = new Vector3 (Interface.ListRessources [ListN].transform.position.x, 0, Interface.ListRessources [ListN].transform.position.z);
destination = true;
} else if (Interface.ListRessources.Count > ListN) {
ListN = ListN + 1;
RTarget ();
} else {
Debug.Log ("teste fonctionement Rdistance +1");
RDistance = RDistance + 1;
ListN = 0;
RTarget ();
}
}
void Deplacement(){ // déplacement de L'unité en fonction du nombre
if (UniteNcalcul > 5) {
UniteNcalcul = UniteNcalcul - 5;
posz = posz + 2;
Deplacement ();
} else {
if (UniteNcalcul == 1) {
agent.destination = hit.point + new Vector3 (0, 0, posz);
} else if (UniteNcalcul == 2) {
agent.destination = hit.point + new Vector3 (2, 0, posz);
} else if (UniteNcalcul == 3) {
agent.destination = hit.point + new Vector3 (-2, 0, posz);
} else if (UniteNcalcul == 4) {
agent.destination = hit.point + new Vector3 (4, 0, posz);
} else if (UniteNcalcul == 5) {
agent.destination = hit.point + new Vector3 (-4, 0, posz);
}
}
}
}
Code : Tout sélectionner
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class Interface : MonoBehaviour {
public static int Charbon;
public static int fer;
public static int Aluminium;
public static int Cuivre;
public static int Or;
public static int Diamant;
public Text RessourceText;
public static List<GameObject> ListRessources = new List<GameObject>();
public int ListR;
void Start () {
}
void Update () {
RessourceText.text = ( "Charbon : " + Charbon + " | Fer : " + fer + " | Aluminium : " + Aluminium + " | Cuivre : " + Cuivre + " | Or : " + Or + " | Diamant : " + Diamant);
}
}
L'unité à <Transporteur = true> dans l'inspector.
Quand je lance le jeux sur unity, la console me dit :
Code : Tout sélectionner
RTarget () _ recherche : False | destination : False | ListN : 0 | RDistance : 1 | 3
UnityEngine.Debug:Log(Object)
Unites:RTarget() (at Assets/Game/Joueur/Unites/Unites.cs:224)
Unites:Update() (at Assets/Game/Joueur/Unites/Unites.cs:131)
RTarget () _ recherche : False | destination : False | ListN : 1 | RDistance : 1 | 3
UnityEngine.Debug:Log(Object)
Unites:RTarget() (at Assets/Game/Joueur/Unites/Unites.cs:224)
Unites:RTarget() (at Assets/Game/Joueur/Unites/Unites.cs:230)
Unites:Update() (at Assets/Game/Joueur/Unites/Unites.cs:131)
RTarget () _ recherche : False | destination : False | ListN : 2 | RDistance : 1 | 3
UnityEngine.Debug:Log(Object)
Unites:RTarget() (at Assets/Game/Joueur/Unites/Unites.cs:224)
Unites:RTarget() (at Assets/Game/Joueur/Unites/Unites.cs:230)
Unites:RTarget() (at Assets/Game/Joueur/Unites/Unites.cs:230)
Unites:Update() (at Assets/Game/Joueur/Unites/Unites.cs:131)
RTarget () _ recherche : False | destination : False | ListN : 3 | RDistance : 1 | 3
UnityEngine.Debug:Log(Object)
Unites:RTarget() (at Assets/Game/Joueur/Unites/Unites.cs:224)
Unites:RTarget() (at Assets/Game/Joueur/Unites/Unites.cs:230)
Unites:RTarget() (at Assets/Game/Joueur/Unites/Unites.cs:230)
Unites:RTarget() (at Assets/Game/Joueur/Unites/Unites.cs:230)
Unites:Update() (at Assets/Game/Joueur/Unites/Unites.cs:131)
ArgumentOutOfRangeException: Argument is out of range.
Parameter name: index
System.Collections.Generic.List`1[UnityEngine.GameObject].get_Item (Int32 index) (at /Users/builduser/buildslave/mono/build/mcs/class/corlib/System.Collections.Generic/List.cs:633)
Unites.RTarget () (at Assets/Game/Joueur/Unites/Unites.cs:225)
Unites.RTarget () (at Assets/Game/Joueur/Unites/Unites.cs:230)
Unites.RTarget () (at Assets/Game/Joueur/Unites/Unites.cs:230)
Unites.RTarget () (at Assets/Game/Joueur/Unites/Unites.cs:230)
Unites.Update () (at Assets/Game/Joueur/Unites/Unites.cs:131)
ps : mon script contient surement d'autres erreur de débutant, mais je bloque surtout sur elle.