- Home /
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);
}
}
}
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.
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.
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!
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.. :-/
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";
}