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 g__l · Feb 17, 2016 at 04:55 PM · instantiateprefablist

Game object instantiated and added to list but the change in capacity shows that 4 game objects have been added to the list?

Pretty much as the title says. So my list capacity was not adding up when i checked it at the end of my code, but seemed fine in the inspector, so i broke it down and found an anomaly where my list capacity doubled. So first i made it where in the code above i instantiated an the object not to be the same object instantiated again (hence empty2). Did not work. So i thought that the instantiation duplication may be due to both the object being exactly the same, empty game objects, so i tagged empty2 with a new tag of empty 2. Still did not work! So here i am asking you if you know what is happening here. Thank you for any help. Apologies in advance for the messiness of my code (below).

 using UnityEngine;
 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine.UI;
 
 
 public class PolyDrawExample : MonoBehaviour
 {
     public int numberOfSides;
     public float polygonRadius;
     public Vector2 polygonCenter;
     public GameObject empty;
     public GameObject empty2;
     List<GameObject> Lines;
     Line Line;
 int i;
 List<Vector3> Vertices;
 Vector2 currentCorner;
 public GameObject button;
 public GameObject buttonObj;
 new public Camera camera;
 public Transform canvas;
 Vector3 buttonPressed;
 int i2;
 List<GameObject> Buttons;
 int i3;

 void Start()
 {
     numberOfSides = 5;
     polygonRadius = 2.5f;
     i = 1;
     polygonCenter = new Vector2(0, 0);
     Lines = new List<GameObject>();
     Vertices = new List<Vector3>();
     Buttons = new List<GameObject>();
     Line = GetComponent<Line>();
     DrawPolygon(polygonCenter, polygonRadius, numberOfSides);
 }

 void Update()
 {
     i2 = 0;
     while (numberOfSides + 1 > i2)
     {
         if (Buttons[i2].GetComponent<ButtonPressControl>().click == true)
         {
             Debug.Log(Buttons[i2].name + " is pressed");
         }
         i2 = i2 + 1;
     }

 }

 void DrawPolygon(Vector2 center, float rad, int numSides)
 {
     Vector2 startCorner = new Vector2(rad, 0) + polygonCenter;
     Vector2 previousCorner = startCorner;

     for (i = 1; i < numSides; i++)
     {
         float cornerAngle = 2f * Mathf.PI / (float)numSides * i;
         currentCorner = new Vector2(Mathf.Cos(cornerAngle) * rad, Mathf.Sin(cornerAngle) * rad) + polygonCenter;
         // create empty game object
         GameObject lin = Instantiate(empty) as GameObject;
         // name it with number
         lin.name = "Line " + i;
         // add to lines list
         Lines.Add(lin);
         // create new line (made class in Line). Attach game object and corners and colours
         Line.line line = new Line.line(Lines[i - 1], currentCorner, previousCorner, Color.blue);
         // attach the linerenderer to game object with said info from above
         Line.setupline(line);

         // Having used the current corner, it now becomes the previous corner.
         previousCorner = currentCorner;

         Vertices.Insert(i - 1, previousCorner);
     }
     // lines list capacity is 4
     // Draw the final side by connecting the last corner to the starting corner.
     GameObject li = Instantiate(empty2) as GameObject;
     li.name = "Line " + i;
     Debug.Log(Lines.Capacity);
     // is 4 as should be      LOOK HERE    LOOK HERE    LOOK HERE   LOOK HERE   LOOK HERE - obvious enough i hope... :(
     Lines.Add(li);
     Debug.Log(Lines.Capacity);
     // is 8???               LOOK HERE    LOOK HERE   LOOK HERE   LOOK HERE   LOOK HERE
     Line.line linee = new Line.line(Lines[i - 1], startCorner, previousCorner, Color.blue);
     Line.setupline(linee);
     Vertices.Insert(i - 1, startCorner);

     GameObject sphere = GameObject.CreatePrimitive(PrimitiveType.Sphere);
     sphere.transform.position = Vector3.zero;
     sphere.transform.localScale = new Vector3(0.2f, 0.2f, 0.2f);
     sphere.GetComponent<Renderer>().material = new Material(Shader.Find("GUI/Text Shader"));
     sphere.GetComponent<Renderer>().material.color = Color.blue;
     i = 1;

     //Creates center lines
     while (i < numSides + 1)
     {
         GameObject li2 = Instantiate(empty) as GameObject;
         li2.name = "Line " + (i + numberOfSides);
         Lines.Add(li2);
         Line.line linee2 = new Line.line(Lines[i + numSides - 1], Vector3.zero, Vertices[i - 1], Color.blue);
         Line.setupline(linee2);
         i = i + 1;
     }
     i = 1;
     while(i < numSides + 1)
     {
         GameObject sphere2 = GameObject.CreatePrimitive(PrimitiveType.Sphere);
         sphere2.transform.position = Vertices[i - 1];
         sphere2.transform.localScale = new Vector3(0.1f, 0.1f, 0.1f);
         sphere2.GetComponent<Renderer>().material = new Material(Shader.Find("GUI/Text Shader"));
         sphere2.GetComponent<Renderer>().material.color = Color.blue;
         i = i + 1;
     }

     Vertices.Add(Vector3.zero);
     i = 1;
     while (i < numSides + 2)
     {
         GameObject Button = Instantiate(button) as GameObject;
         Button.transform.SetParent(canvas, false);
         Button.transform.position = camera.WorldToScreenPoint(Vertices[i - 1]);
         Button.name = "Button " + i;
         Buttons.Add(Button);
         i = i + 1;
     }
 }

}

Ideas of what is causing the problem -

  • As in the inspector there is no other game objects maybe the error relates to the .Add function on the list

Comment
Add comment · Show 1
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 g__l · Feb 17, 2016 at 05:06 PM 0
Share

The important part is highlighted with 'LOO$$anonymous$$ HERE's

1 Reply

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

Answer by Eno-Khaon · Feb 17, 2016 at 10:45 PM

A List<> needs to allocate memory in order to use it, but unlike an Array, it doesn't specifically have to do so all at once.

In exchange for this, Lists allocate chunks of memory at a time in groups, rather than only for each individual element at once.

Therefore, what you're seeing when querying the List<> is the total memory allocated for the list, or its Capacity. To return the total number of elements you've added to your list at present, you would instead use Count.

Edit: In other words, Capacity is the total space available in the List at present, whereas Count is the total space you've used of that capacity.

Comment
Add comment · Show 1 · 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 g__l · Feb 18, 2016 at 10:28 AM 0
Share

Thank you , that makes sense.

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

41 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

Related Questions

A node in a childnode? 1 Answer

What is the best way to convert a string list to a class list? 1 Answer

How do I Instantiate a prefab in a list? 0 Answers

How to make TCG Deck (Instatiate based on Prefab) 1 Answer

How to instantiate a prefab one by one in foreach loop 3 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