Search Unity

Dynamic creation of Spheres in C#

Discussion in 'iOS and tvOS' started by arian487, Aug 7, 2009.

  1. arian487

    arian487

    Joined:
    Aug 4, 2009
    Posts:
    27
    I'm having a lot of trouble doing this. Basically I want to randomly generate an unset number of spheres on screen with different positions. All the nodes are exactly the same, except for positions. Here is how I tried to do it:

    for (int i = 0; i<7; i++)
    {
    Vector3 v = new Vector3 (RandomNumber (10, 471), RandomNumber (10, 310), 0);
    GameObject s = new GameObject ();
    s = GameObject.CreatePrimitive (PrimitiveType.Sphere);
    s.transform.position = v;
    myNodes.Add (s);
    }

    All examples I saw were done using JS and I'd MUCH rather use C#. myNodes is just an ArrayList but this isn't working. Any ideas?
     
  2. arian487

    arian487

    Joined:
    Aug 4, 2009
    Posts:
    27
    So it seems my error is coming when I'm trying to add the object to the arraylist. When I comment that out it works. However, I definitely need to keep track of my nodes so what am I doing wrong?
     
  3. n0mad

    n0mad

    Joined:
    Jan 27, 2009
    Posts:
    3,732
    Code (csharp):
    1.  
    2.  
    3. // C# code
    4.  
    5. //declaration outside your loops for better perfs
    6. private GameObject s;
    7.  
    8.  
    9.  
    10. for (int i = 0; i<7; i++)
    11. {
    12.  
    13. s = GameObject.CreatePrimitive(PrimitiveType.Sphere);
    14. s.transform.position = new Vector3 ((Random.value*461)+10, (Random.value*300)+10, 0F);
    15.  
    16. myNodes.Add (s);           
    17. }
    18.  

    This code seems correct to me, the error might come from myNodes.

    I don't use ArrayList, but Hashtables. Still, with Hashtables you got to specify the index of each member you add. If myNodes was a Hashtable, would be :

    Code (csharp):
    1. myNode.Add(i, s); // i is the incrementation number
     
  4. refresh

    refresh

    Joined:
    May 14, 2009
    Posts:
    20
    are you using the using System.Collections namespace? i think that is needed for ArrayList.


    Code (csharp):
    1. using System.Collections;
    2. using UnityEngine;
     
  5. arian487

    arian487

    Joined:
    Aug 4, 2009
    Posts:
    27
    Yes I am using the collections namespace.
     
  6. Bjerre

    Bjerre

    Joined:
    Nov 8, 2006
    Posts:
    108
    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4.  
    5. public class CreateSphere : MonoBehaviour {
    6.  
    7.     private GameObject s;
    8.     private ArrayList myNodes;
    9.    
    10.     void Start ()
    11.     {
    12.         myNodes = new ArrayList();
    13.        
    14.         for (int i = 0; i < 7; i++)
    15.         {
    16.             s = GameObject.CreatePrimitive(PrimitiveType.Sphere);
    17.             s.transform.position = new Vector3 ((Random.value*461)+10, (Random.value*300)+10, 0F);
    18.            
    19.             myNodes.Add(s);
    20.         }
    21.     }
    22. }
    23.  
    this works for me

    did you remember to create your arraylist ?
     
  7. MugOfPaul

    MugOfPaul

    Joined:
    Jun 13, 2008
    Posts:
    34
    What do you mean by "isn't working"? Is isn't compiling or it's running and you're not seeing anything? When you run in the editor, do you see the GameObject pop up in the scene hierarchy? I'm wondering if the scale is so small you're just not seeing them.

    To be honest, I typically do this sort of thing by instantiating prefabs.
     
  8. arian487

    arian487

    Joined:
    Aug 4, 2009
    Posts:
    27
    Yes, I meant isn't compiling. But thanks your code shed light on my idiocy! I forgot to actually CREATE the object (I was only declaring it). Damn C, so used to pointers and allocations that I forgot you need to do this.
     
  9. isaac_weisberg

    isaac_weisberg

    Joined:
    May 2, 2017
    Posts:
    2
  10. jmtBASAD

    jmtBASAD

    Joined:
    Dec 25, 2017
    Posts:
    1
    hi - thanks for this helpful discussion. i am try to do something similar based on the nodes of an xml file. could you explain the solution to "I forgot to actually CREATE the object" - how should i do this?