- Home /
New to unity - I am using this script to add prefabs to an inventory array on click. How can I remove those prefabs/gameobjects from my array slots once I have instantiated them? see the example code below:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Addonclick : MonoBehaviour
{
public List<GameObject> prefabs;
public Transform[] spawnLocations;
public RaycastHit hit;
public Ray ray;
public float YAxisAngle;
void Start()
{
}
void Update()
{
for (int i = 0; i < prefabs.Count; i++)
{
if (prefabs[i] == null)
{
prefabs.RemoveAt(i);
}
}
if (Input.GetMouseButtonDown(0))
{
ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if (Physics.Raycast(ray, out hit, 100))
{
hit.rigidbody.gameObject.tag = "Curated";
Destroy(hit.rigidbody.gameObject.GetComponent<PaintingLife>());
addtoArray(hit.rigidbody.gameObject);
Debug.Log(prefabs);
//hit.rigidbody.gameObject.transform.position = new Vector3(0, -100, 0);
hit.rigidbody.gameObject.SetActive(false);
hit.rigidbody.gameObject.GetComponent<PaintingLongLife>().enabled = (true);
//Destroy(hit.rigidbody.gameObject);
//Debug.Log(hit.collider.gameObject.name);
}
}
if (Input.GetMouseButtonDown(2))
{
instantiateList();
}
}
void addtoArray(GameObject obj)
{
prefabs.Add(obj);
}
void instantiateList()
{
for (int i = 0; i < prefabs.Count; i++)
{
prefabs[i].SetActive(true);
Instantiate(prefabs[i], spawnLocations[0].transform.position, Quaternion.Euler(-90, YAxisAngle, 0));
}
}
}
Answer by hameed-ullah-jan · Nov 27, 2018 at 06:42 AM
In the following function just add this condition, this way you cannot add the same objects to the list:
void addtoArray(GameObject obj)
{
if(!prefabs.Contains(obj))
prefabs.Add(obj);
}
This doesn't seem to be working - for more clarity: Once the player has added an item to the array, with the function (Input.Get$$anonymous$$ouseButtonDown(0)) later on, they will have the chance to instantiate the objects somewhere else with the function (Input.Get$$anonymous$$ouseButtonDown(2)). which calls the instantiateList().
Once the list is instantiated, I need that list to essentially be reset.
if you want to reset the list, you can just use prefabs.Clear()
You sir are awesome!! I was able to add prefabs.Clear(); to the (Input.Get$$anonymous$$ouseButtonDown(2)) command and it worked. It gave me a bug where if I clicked on that same obect and spawned it again, it spit out multiple duplicates. But I fixed this by adding the requirement if (Physics.Raycast(ray, out hit, 100) & hit.rigidbody.gameObject.tag == "Painting") to the (Input.Get$$anonymous$$ouseButtonDown(0)). This way the script only acted on objects with the pre-existing tag.
Thank you so much!
Your answer
Follow this Question
Related Questions
Automatic Cloning on Instantiation 1 Answer
Three Spots For Three Random Objects 1 Answer
How should i add raycast to multiple game objects??? 0 Answers
getting .Transform from instanntiated object 2 Answers
How to remove objects from a list ? 3 Answers