- Home /
Question by
FuturPlanet · Jan 04 at 08:31 PM ·
c#prefaboverride
Prefab values get saved unwantedly
Hello there. Im having the wierd situation where my prefabs get overriden. The get instantiated, some values slaped on and after stopping the game the values stay on my prefab. That includes bools, ints, strings. etc. Any idea why? Was it always like this and I just never realized?
How its supposed to look:
How it looks after running the game:
Here is the script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using TMPro;
public class MoleculeSpawn : MonoBehaviour
{
//Components
private MoleculePrint MP;
private Elements E;
private CurrentMolecule CM;
[SerializeField]
private GameObject ENC1;
[SerializeField]
private GameObject ENC2;
[SerializeField]
private GameObject ENC3;
[SerializeField]
private GameObject ENC4;
[SerializeField]
private GameObject ENC5;
[SerializeField]
private GameObject ENC6;
[SerializeField]
private GameObject ENC7;
[SerializeField]
private GameObject ENC8;
public uint ID;
private string Name;
private string Atomic_Symbol;
private decimal Molecular_Weight;
private byte Effective_Nuclear_Charge;
private bool Radioactive;
private decimal Density;
private void Awake()
{
MP = GameObject.FindGameObjectWithTag("AtomController").GetComponent<MoleculePrint>();
E = MP.GetComponent<Elements>();
CM = E.GetComponent<CurrentMolecule>();
}
public void Spawn()
{
Name = E.NE[ID];
Atomic_Symbol = E.AS[ID];
Molecular_Weight = E.MW[ID];
Effective_Nuclear_Charge = E.EC[ID];
Radioactive = E.RA[ID];
Density = E.DY[ID];
GameObject molecule;
if(Effective_Nuclear_Charge == 1)
{
molecule = ENC1;
}
else if (Effective_Nuclear_Charge == 2)
{
molecule = ENC2;
}
else if (Effective_Nuclear_Charge == 3)
{
molecule = ENC3;
}
else if (Effective_Nuclear_Charge == 4)
{
molecule = ENC4;
}
else if (Effective_Nuclear_Charge == 5)
{
molecule = ENC5;
}
else if (Effective_Nuclear_Charge == 6)
{
molecule = ENC6;
}
else if (Effective_Nuclear_Charge == 7)
{
molecule = ENC7;
}
else if (Effective_Nuclear_Charge == 8)
{
molecule = ENC8;
}
else
{
Debug.LogError("Effective Nuclear Charge is outside of bounds!");
molecule = ENC1;//Cant have empty situations!
}
molecule.name = Name;
molecule.transform.Find("Circle/Text (TMP)").GetComponent<TMP_Text>().text = Atomic_Symbol;
CM.Molecular_Weight += Molecular_Weight;
if (Radioactive)
{
CM.Radioactive = true;
}
CM.Density += Density;
CM.AtomCount++;
AtomID aid = molecule.transform.Find("Circle").GetComponent<AtomID>();
aid.ID = CM.AtomCount;
aid.conCount = Effective_Nuclear_Charge;
Instantiate(molecule, new Vector3(0, 0, 0), Quaternion.identity);
if(CM.AtomCount == 1)
{
aid.core = true;
}
aid.AddAtom();
}
}
prefab-bug-001.png
(140.8 kB)
prefab-bug-002.png
(137.2 kB)
Comment
Best Answer
Answer by Casiell · Jan 04 at 11:35 PM
You need to instantiate the prefab first, and only then modify the values on the actual instance.
You are changing the properties on 'molecule' prefab BEFORE instantiating.
Instantiate method actually returns the instance, so you need to assign it to a new variable and make all the changes on that instance