Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 SavageX370 · Nov 27, 2018 at 05:11 AM · scripting problemsizearray of gameobjectsupdate problem

New to unity/scripting: How can I update the size of my Gameobject Array so that it will not include missing prefabs/Gameobjects?

 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 ()
     {
         
     }
     
     private void Update ()
     {
         if (Input.GetMouseButtonDown(0))
         {
             ray = Camera.main.ScreenPointToRay(Input.mousePosition);
             if (Physics.Raycast(ray, out hit, 100))
             {
                 hit.rigidbody.gameObject.tag = "Painting";
                 Destroy(hit.rigidbody.gameObject.GetComponent<PaintingLife>());
                 addtoArray(hit.collider.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 05:29 AM

you can check if prefabs[i] is null, try prefabs.Remove (name); or prefabs.RemoveAt (_listCntr);

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 05:30 AM 0
Share

Which line/update would that be appropriate in?

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

you can add this code in this function:` void instantiateList() {

for (int i = 0; i < prefabs.Count; i++) { if(prefabs[i] == null){ prefabs.RemoveAt(i); }

      for (int i = 0; i < prefabs.Count; i++)
      {


          prefabs[i].SetActive(true);
          Instantiate(prefabs[i], spawnLocations[0].transform.position, Quaternion.Euler(-90, YAxisAngle, 0));
 
      }
  }`
avatar image SavageX370 · Nov 27, 2018 at 06:08 AM 0
Share

Thank you so much - this was the answer!!! Although I needed to put it inside of the void update function so that it would clear all the empty slots consistently, and not just one slot at a time on click.

Really appreciated the help - you folks are true angels

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

169 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

Related Questions

Size over lifetime and particle collisions 0 Answers

How to deactivate an object in array which was chosen randomly before then activate randomly another one also in that array? 1 Answer

"CameraClearFlags.Nothing" does not work? 2 Answers

How to create an array of prefabs with a specific script attached 3 Answers

Mesh Renderer Matereal Aray reduce? 0 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