Hello,
I’m trying to replace the script of an instantiated gameobject with one of the same type I have stored in a List. Here’s the code, which is throwing an error on the final instruction.
The error is: The object of type turret gun scripthas been destroyed but you are still trying to access it
Is this sort of script replacement possible? And if so, is it a good idea?(i.e. won’t cause unexpected behaviour) Thanks for your time!
// Instantiate and get reference to Gameobject
GameObject tempTurret = (GameObject)Instantiate(turretPrefab2, turretScriptList*.GetTurretPosition(), Quaternion.identity);*
-
if(tempTurret == null)*
-
{*
-
Debug.Log("TurretSceneScript1.PlaceExistingTurrets: Turret1 is null"); *
-
}*
-
// Get the current turret's script*
-
TurretGunScript currentTurretScript = (TurretGunScript) tempTurret.transform.Find("Gun").GetComponent("TurretGunScript");*
-
// Get the stored Script in tempsave*
_ TurretGunScript replacementScript = (TurretGunScript) turretScriptList*;*_
* // Replace the current script*
* currentTurretScript = replacementScript;*
* // Set the texture appropriate to that type*
* currentTurretScript.SetTurretTexture();*