Dans mon code j'essaie d'instancier puis de déplacer des gameobject en faisant appel à un script à l'intérieur de ces gameobject.
J'ai un premier gameobject vide dont le script "CreateCells" contient l'instanciation de plusieurs prefab :
Code : Tout sélectionner
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CreateCells : MonoBehaviour {
public GameObject Cell;
GameObject CellClone;
Vector3 pos;
// Use this for initialization
void Start () {
for (int i = 0; i < 5; i++) {
CellClone = Instantiate (Cell) as GameObject;
pos.x = -5;
pos.y = 0;
pos.z = 0;
CellClone.transform.position = pos;
pos.x = -5+2*i;
pos.y = 5;
pos.z = 0;
Debug.Log ("X :" + pos.x + " Y :" + pos.y + " Z :" + pos.z);
CellClone.GetComponent<MoveCell> ().MoveC (pos);
}
}
// Update is called once per frame
void Update () {
}
}
Code : Tout sélectionner
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class MoveCell : MonoBehaviour {
public Vector3 target;
float speed;
float step;
// Use this for initialization
void Start () {
target = new Vector3 ();
target = this.gameObject.transform.position;
speed = 100f;
}
public void MoveC (Vector3 move)
{
target = move;
}
// Update is called once per frame
void Update () {
if (target != this.gameObject.transform.position ){
step = speed * Time.deltaTime;
this.gameObject.transform.position = Vector3.MoveTowards (this.gameObject.transform.position, target, step);
}
}
}
Malheureusement ce "MoveCell" n'est jamais exécuté et je ne comprend pas du tout pourquoi.
Si vous aviez une idée de ce qu'il se passe et de comment contourner le problème j'en serais ravi
Cordialement,
Kobal