Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
Question by Dovafox · Jul 01, 2020 at 05:50 AM · instantiatedestroy

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.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0

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();
         }
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

235 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges