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 /
avatar image
0
Question by Dothackking · Jul 11, 2013 at 11:27 PM · c#destroyinventory

Inventory problems (C#) [edited to be more clear]

Swapping weapons now creates a different clone each switch, and they don't get pruned, if I swap 6 times, there's 5 disabled weapons. Any tips?

 using UnityEngine;
 using System.Collections;
 using System.Collections.Generic;
 
 public class Inventory : MonoBehaviour {
     public System.Collections.Generic.List<GameObject> inventory;
     int x=0;
     GameObject Weapon;
     public GameObject WepHolder;
     GameObject yourWeapon;
     GameObject clone1;
     // Use this for initialization
     void Start () {
     }
     
     // Update is called once per frame
     void Update () {
     Weapon = GameObject.FindWithTag("weaponEquipped");
     if(Input.GetAxis("Mouse ScrollWheel") < 0 && x > 0) //back
         {
             Weapon.SetActive(false);
             Weapon.tag = "weapon";
             x--;
             clone1 = Instantiate(inventory[x], WepHolder.transform.position, WepHolder.transform.rotation) as GameObject;
             clone1.transform.parent = GameObject.Find ("Main Camera").transform;
             clone1.SetActive (true);
             clone1.tag = "weaponEquipped";
         }
     if(Input.GetAxis("Mouse ScrollWheel") > 0) //forward
         {
             Weapon.SetActive(false);
             Weapon.tag = "weapon";
             x++;
             clone1 = Instantiate(inventory[x], WepHolder.transform.position, WepHolder.transform.rotation) as GameObject;
             clone1.transform.parent = GameObject.Find ("Main Camera").transform;
             clone1.SetActive (true);
             clone1.tag = "weaponEquipped";
         }
     }
     
     void OnTriggerEnter(Collider col)
     {
         if(col.tag == "groundwep")
         {
             col.tag = "weapon";
             inventory.Add (col.gameObject);
             col.gameObject.SetActive(false);
         }
     }
 }
 
Comment
Add comment · Show 4
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
avatar image Lairinus · Jul 12, 2013 at 12:52 AM 0
Share

This is what I got from it:

  • yourWeapon is the weapon you're currently using.

  • Weapon is a prefab

  • clone1 is the instantiated item.

Calling Destroy() gets rid of the object original, IF it's assigned to the object original. To avoid this, assign Weapon to another variable such as weaponRef, and destroy that ins$$anonymous$$d.

That being said, I really don't know what you're trying to do here, mainly because it's kind of hard to read. It seems that I'm looking at half of the actual code.

IF what you're doing is assigning variables from the list as the "weapon" and then calling Destroy() on the weapon, then I can tell you that doing so will destroy every trace of the assigned value. Refer to two paragraphs prior.

Please clarify if none of this helps you.

avatar image Dothackking · Jul 12, 2013 at 04:24 PM 0
Share

I edited it to be more clear, since I had already solved that. I had probably done so while you were typing your reply, but now I'm having a new problem, and I posted it above ins$$anonymous$$d of that problem.

avatar image Dothackking · Jul 12, 2013 at 11:47 PM 0
Share

markedagain, you were right exactly. Thanks to you I figured out how to remove my memory leak! Please post it as an answer so I can choose it as correct!

avatar image SinisterRainbow · Jul 13, 2013 at 05:14 AM 0
Share

u can give him a thumbs up and close the thread if he doesn't respond.. i read this whole thing before discovering it was answered.. :-/

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by markedagain · Jul 12, 2013 at 06:28 PM

looking at your code, what you describe seems to be normal.

where do u destroy the object? should there not be a Destroy(clone1) right before creating a new one?

also u can prob clean up the code by moving duplicate actions out of the if functinos

 bool toUpdate = false;
 if(Input.GetAxis("Mouse ScrollWheel") < 0 && x > 0) //back
 {
     x--;
     toUpdate = true;
 }
 if(Input.GetAxis("Mouse ScrollWheel") > 0) //forward
 {
     x++;
     toUpdate = true;
 }
 if (toUpdate){
     Weapon.SetActive(false);
     Weapon.tag = "weapon";
     
     clone1 = Instantiate(inventory[x], WepHolder.transform.position, WepHolder.transform.rotation) as GameObject;
     clone1.transform.parent = GameObject.Find ("Main Camera").transform;
     clone1.SetActive (true);
     clone1.tag = "weaponEquipped";
 }
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

18 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

Related Questions

Is there any easy way to keep buttons inside of box? (c#) 1 Answer

Unity "unequip" selected item from inventory. 1 Answer

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

Distribute terrain in zones 3 Answers

Multiple Cars not working 1 Answer


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