• 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 SavageX370 · Nov 27, 2018 at 06:15 AM · gameobjectinstantiateupdateremovearray of gameobjects

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));
 
         }
     }
 }
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
1
Best Answer

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);
      }
Comment
Add comment · Show 3 · 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
avatar image SavageX370 · Nov 27, 2018 at 07:01 AM 0
Share

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.

avatar image hameed-ullah-jan · Nov 27, 2018 at 07:04 AM 1
Share

if you want to reset the list, you can just use prefabs.Clear()

avatar image SavageX370 hameed-ullah-jan · Nov 27, 2018 at 07:31 AM 0
Share

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

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

157 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

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

Continually Updating position of Instantiated Object (to Mouse Pointer) 0 Answers

Spawning GameObject contained within another 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