Je rencontre actuellement un problème qui ne doit pas être compliqué (d'ailleurs j'ai déjà trouvé une solution mais sale...)
Alors voilà le problème, je souhaite créer un GameObject et le modifier puis l'instancier sauf que je n'y arrive pas car la modification se passe bien mais avec le Start() de l'instanciation... Les modifs sont reset !
De plus, je ne peux pas modifier le Renderer sans que l'objet soit crée.
Voici la classe qui génère tout ça pour mieux comprendre :
Code : Tout sélectionner
public class EggGenerator : MonoBehaviour
{
private EggConfig config;
public GameObject eggPrefab;
void Start()
{
config = GameObject.Find("Sun").GetComponent<EggConfig>();
}
public void GiveEgg(Queenbehaviour queen, int prob = -1)
{
Egg egg = eggPrefab.GetComponent<Egg>();
//Generate type and attribut
float minus = this.GenerateEgg(egg, prob);
if (minus != -1)
{
if (queen.curEnergy >= minus)
{
queen.curEnergy -= minus;
Instantiate(eggPrefab, queen.spawner.transform.position, Quaternion.identity);
}
}
}
public float GenerateEgg(Egg egg, int prob)
{
float costEgg = 0;
//If no buff for probabilities
if (prob == -1)
{
costEgg = this.LegitGenerete(egg);
}
else
{
costEgg = this.BoostGenerete(egg, prob);
}
return costEgg;
}
public float RandomGenerator()
{
return Random.Range(0f, config.GetMaxProba() + config.GetFailproba());
}
private float LegitGenerete(Egg egg)
{
float rateSpawn = this.RandomGenerator();
foreach (EggState state in this.GetListProba())
{
if (rateSpawn >= state.min && rateSpawn <= state.max)
{
return this.InitEgg(egg, state.indTypeScrum);
}
}
return -1;
}
private float BoostGenerete(Egg egg, int typeOfScrumBoost)
{
float rateSpawn = this.RandomGenerator();
//One chance for 4 to create the needed egg
if (rateSpawn >= config.GetBoostBegin() && rateSpawn <= config.GetBoostEnd())
{
return this.InitEgg(egg, typeOfScrumBoost);
}
else
{
return this.LegitGenerete(egg);
}
}
private float InitEgg(Egg egg, int index)
{
egg.energieToEvolve = this.config.GetCost(index);
egg.iAmThis = this.config.GetScrumType(index);
//egg.rend.material.color = this.config.GetColor(index);
return this.config.GetCostEgg(index);
}
private EggState[] GetListProba()
{
EggState[] list = new EggState[EggConfig.sizeOfTypeScrum];
float max, min = 0;
float limit = config.GetMaxProba();
int total = 0, i = 0;
//Calcul of total
foreach (int proba in config.scrumProba.Values)
{
total += proba;
}
//Calcul of proba by type of scrum
foreach (KeyValuePair<int, int> entry in config.scrumProba)
{
max = (entry.Value * limit) / total;
list[i] = new EggState(entry.Key, min, max);
min = max + 1;
i++;
}
return list;
}
}
Vous pouvez voir que je ne l'instancie que à la fin cependant... Le start() détruit tout...