Deleting an instantiated object on a different if statement,Destroy an instantiated object on a different if statement
Hi, I am currently trying to make it so I can equip and unequip. In this case I use instantiate for equipping the gun and then "destroy" to unequip it. Problem is that since there is no references to that "gameobject" which has been declared inside the equip if statement, it would of course not be able to destroy it.
Here is the code using System.Collections; using System.Collections.Generic; using UnityEngine;
public class WeaponsSystem : MonoBehaviour
{
[Header("Weapon System")]
public GameObject gunSpawnLocation;
public GameObject sampleGun;
public Transform playerCamera;
Vector3 gunSpawn;
Quaternion playerCameraRotation;
string currentlyEquipped;
//bool isEquipped;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
gunSpawn = gunSpawnLocation.transform.position;
playerCameraRotation = playerCamera.transform.rotation;
//Weapons system
if (Input.GetKeyDown("1") && currentlyEquipped != "Pistol")
{
currentlyEquipped = "Pistol";
//Spawning & Parenting
GameObject pistol = (GameObject)Instantiate(sampleGun, gunSpawn, playerCameraRotation);
pistol.transform.parent = playerCamera;
print("Pistol is equipped");
}
if (Input.GetKeyDown("1") && currentlyEquipped == "Pistol")
{
Destroy(pistol);
}
}
}
My question to this is how do I solve this? Can I "access" that GameObject somehow from outside it?
Thanks.
Answer by Dovafox · Jul 05, 2020 at 08:43 AM
After some time I managed to get something that works. I also thought about how instances would react to "limited" ammunition, so I created a float on the Player named "pistolMagazine" and directly referenced it to the pistol via code.
Functions are more handy for code like this.
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class WeaponsSystem : MonoBehaviour
{
[Header("Default")]
public GameObject gunSpawnLocation;
public Transform playerCamera;
GameObject currentWeapon;
Quaternion playerCameraRotation;
Vector3 gunSpawn;
string currentlyEquipped;
bool isEquipped;
bool hasItem;
//All gun objects
[Header("Weapons")]
public GameObject sampleGun;
public GameObject stickWeapon;
[Header("Magazines")]
public float pistolMagazine = 12f;
// Start is called before the first frame update
void Start()
{
}
void spawnPistol()
{
Destroy(currentWeapon);
GameObject pistol = Instantiate(sampleGun, gunSpawn, playerCameraRotation) as GameObject;
pistol.transform.parent = playerCamera;
currentWeapon = pistol;
currentlyEquipped = "pistol";
isEquipped = true;
}
void destroyPistol()
{
currentlyEquipped = "";
isEquipped = false;
Destroy(currentWeapon);
}
void spawnStick()
{
Destroy(currentWeapon);
isEquipped = true;
GameObject stick = Instantiate(stickWeapon, gunSpawn, playerCameraRotation) as GameObject;
stick.transform.parent = playerCamera;
currentWeapon = stick;
currentlyEquipped = "stick";
}
void destroyStick()
{
currentlyEquipped = "";
isEquipped = false;
Destroy(currentWeapon);
}
// Update is called once per frame
void Update()
{
//Weapons system\\
//Variables
gunSpawn = gunSpawnLocation.transform.position;
playerCameraRotation = playerCamera.transform.rotation;
//Pistol\\
if (Input.GetKeyDown("1") && currentlyEquipped != "pistol")
{
spawnPistol();
}
else
if (Input.GetKeyDown("1") && currentlyEquipped == "pistol")
{
destroyPistol();
}
//Stick\\
if (Input.GetKeyDown("2") && currentlyEquipped != "stick")
{
spawnStick();
}
else
if (Input.GetKeyDown("2") && currentlyEquipped == "stick")
{
destroyStick();
}
Your answer
![](https://koobas.hobune.stream/wayback/20220613002142im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Destroy attached shield to player (C#) 2 Answers
Destroy a prefab from another class 2 Answers
Instantiate and destroy an object with the same key 1 Answer
Problem with Destroy function with instantiated Prefabs 1 Answer
[Solved] Destroy instantiated prefabs with a same tag, one by one, from last instantiated to first ? 2 Answers