- Home /
Gameobject instantiation returns null
So i have a piece of code where i am instantiating an object to use it later as reference to other gameobjects
public GameObject StandardEmpty;
public string NameOfTurret;
public bool IsDouble;
public GameObject T_Bases;
public GameObject T_Swivel;
public List<GameObject> T_Mount = new List<GameObject>();
public List<GameObject> T_Head = new List<GameObject>();
public List<GameObject> T_Breech = new List<GameObject>();
public List<GameObject> T_Barrel = new List<GameObject>();
public List<GameObject> Mount_Connectors = new List<GameObject>();
public List<GameObject> Breech_Connectors = new List<GameObject>();
public TurretManager TurretSaveData;
public TurretScriptable TempSavePoint;
BuilderSync UpdateUI;
TurretScriptable Turret;
GameObject New_Empty;
GameObject New_Bases;
GameObject New_Swivel;
GameObject New_Mount;
GameObject New_Head;
GameObject New_Breech_Center;
GameObject New_Breech_Left;
GameObject New_Breech_Right;
GameObject New_Weapon_Barrel_Center;
GameObject New_Weapon_Barrel_Left;
GameObject New_Weapon_Barrel_Right;
public Vector3 Build_Position;
PartsAssembler PartAssembly;
// i need to make it so it saves the part as soon as its selected not when im done creating the full turret (i think its done)
public void Activation(int TurretIndex) //the turret index is going to tell what scriptable object index its going to save to
{
New_Empty = Instantiate(StandardEmpty.gameObject, Build_Position, Quaternion.identity) as GameObject;
New_Empty.name = NameOfTurret;
Bases(New_Empty);
print(New_Empty.transform);
TurretSaveData.Turrets[TurretIndex].TurretParts.Insert(0, StandardEmpty);
TempSavePoint.TurretParts.Insert(0, StandardEmpty);
PartAssembly.StartSync();
ChangeChecker(TurretIndex);
}
/// <summary>
/// This funtion is the activatior of this script
/// </summary>
and i am getting a null reference at the new_empty
void Bases(GameObject NewEmpty)
{
New_Bases = Instantiate(T_Bases.gameObject, Build_Position, Quaternion.identity, NewEmpty.transform) as GameObject;
Swivel(New_Bases);
TempSavePoint.TurretParts.Insert(1, New_Bases);
}
/// <summary>
/// This funtion creates a new Bases object in the given location
/// </summary>
void Swivel(GameObject Bases)
{
Transform TSwivel = Bases.transform.Find("*SOCKET_SWIVEL");
New_Swivel = Instantiate(T_Swivel.gameObject, TSwivel.position, Quaternion.identity, TSwivel) as GameObject;
TempSavePoint.TurretParts.Insert(2, New_Swivel);
}
/// <summary>
/// This funtion places a new Swivel object in the given socket
/// </summary>
and that carries over to these functions which are chained and it is unclear to me why its returning as a null even thought they get spawned and they are stored in a variable, any help is appreciated.
Answer by AtiyehNorouzi22 · Aug 03, 2021 at 06:58 AM
you have to use "ref" or "out" when you want to pass your gameobject as reference not a copy of variable. you can read link text
if you want to understand why. so in your case this will solve your problem
Bases(ref New_Empty);
void Bases(ref GameObject NewEmpty)
{
New_Bases = Instantiate(T_Bases.gameObject, Build_Position, Quaternion.identity, NewEmpty.transform) as GameObject;
Swivel(New_Bases);
TempSavePoint.TurretParts.Insert(1, New_Bases);
}
/// <summary>
/// This funtion creates a new Bases object in the given location
/// </summary>
void Swivel(GameObject Bases)
{
Transform TSwivel = Bases.transform.Find("*SOCKET_SWIVEL");
New_Swivel = Instantiate(T_Swivel.gameObject, TSwivel.position, Quaternion.identity, TSwivel) as GameObject;
TempSavePoint.TurretParts.Insert(2, New_Swivel);
}
/// <summary>
/// This funtion places a new Swivel object in the given socket
/// </summary>
thanks for trying but that still didnt fix it cs it still gives me an "NullReferenceException: Object reference not set to an instance of an object" error that refers back to "Bases(ref New_Empty);"
@SirZeeno because you have to add ref to swivel as well
Bases(ref New_Empty);
void Bases(ref GameObject NewEmpty)
{
New_Bases = Instantiate(T_Bases.gameObject, Build_Position, Quaternion.identity, NewEmpty.transform) as GameObject;
Swivel(ref New_Bases);
TempSavePoint.TurretParts.Insert(1, New_Bases);
}
/// <summary>
/// This funtion creates a new Bases object in the given location
/// </summary>
void Swivel(ref GameObject Bases)
{
Transform TSwivel = Bases.transform.Find("*SOCKET_SWIVEL");
New_Swivel = Instantiate(T_Swivel.gameObject, TSwivel.position, Quaternion.identity, TSwivel) as GameObject;
TempSavePoint.TurretParts.Insert(2, New_Swivel);
}
/// <summary>
/// This funtion places a new Swivel object in the given socket
/// </summary>
also i couldn't understand why you find another gameobject with name after passing the gameobject as parameter in swirl Bases.transform.Find("*SOCKET_SWIVEL"); this line. also check whether the name is correct. i suspect it has star in the scene.
I did but it's not referring to the swivel it's only referring to the bases. now I am not sure if this is the issue but my bases is a prefab asset of an empty with components attached to it so I don't have to add them in code. and also I am searching for an object in bases that acts as a connector between the 2 objects so they are positioned right and use the transform of that as a parent to swivel.
Your answer
