- Home /
find size of a prefab that is spawned from an array
Hello, I recently started with Unity and C# so I am fairly new and encountered this obstacle I can't find solution for. I am randomly spawning differently sized(i have 6 prefabs) platforms from an array. I want the next platform to be spawned after the previous one with a some kind of gap(that the player is available to jump over) and for this i need to know width of the spawned platform(prefab). The "spawner" itself is working I mean it spawns platforms, but I have problems with position. I searched all over but I don't seem to find anything related to this situation. I tried using bounds.size but i get the following error:
error CS0120: An object reference is required to access non-static member `UnityEngine.Collider2D.bounds'
I attach my current code of the spawner and would really appreciate any help or hints. Thank you in advance.
 using UnityEngine;
 using System.Collections;
 
 public class PlatformSpawnScript : MonoBehaviour {
 
     public GameObject[] platforms;
     public int maxPlatforms = 10;
     public float horizontalMin = 1f;
     public float horizontalMax = 5f;
 
     private Vector2 startingPosition;
 
 
 
     // Use this for initialization
     void Start () {
 
         startingPosition = transform.position;
         spawn ();
 
     
     }
 
     void spawn (){
         for (int i = 0; i < maxPlatforms; i++) {
 
             Instantiate(platforms[Random.Range (0, 5)], startingPosition, Quaternion.identity);
             startingPosition += (Collider2D.bounds.size.x + Random.Range[1f, 5f]);
 
         }
     }
 
 
 
 }
 

First, to access .bounds you need a reference to the object, of which you're trying to find the bounds. This means, you need to create a variable of type Collider2D and fill it either via the inspector or by finding it via code and then you have something like:
 Collider2D lastCollider = platforms[lastIndex]; //or
 public Collider2d collider;
 //and then
 collider.bounds.size.x
Other than that, maybe this tutorial helps: http://unity3d.com/learn/tutorials/modules/beginner/live-training-archive/infinite-runner
 using UnityEngine;
 using System.Collections;
 
 public class PlatformSpawnScript : $$anonymous$$onoBehaviour {
     
     public GameObject[] platforms;
     public int maxPlatforms = 10;
     
     private Vector2 startingPosition;
     
     
     
     // Use this for initialization
     void Start () {
         
         startingPosition = transform.position;
         spawn ();
         
         
     }
     
     void spawn (){
         for (int i = 0; i < maxPlatforms; i++) {
             
             int lastIndex = Random.Range(0, 5);
             GameObject platform = platforms[lastIndex];
             Instantiate(platforms[lastIndex], startingPosition, Quaternion.identity);
             Collider2D lastCollider = platform.GetComponent<Collider2D>();
             startingPosition += new Vector2 ((lastCollider.bounds.size.x + Random.Range(1f, 5f)), 0);
             
         }
     }
     
     
     
 }
 
 
Thank you ,this is how I tried and it kinda works, but platforms still spawn like one on another I mean has one part of a platform on top of another and the next platform also has part of it on another, do I get wrong size or something? if ins$$anonymous$$d of that I try to just add a gap of like 10 or sth, gaps become too big or still longer platforms get on top. $$anonymous$$aybe my code is calculating it wrong?
Answer by jcv8000 · Apr 04, 2015 at 09:46 PM
Collider2D is a component. On line 28 use
GetComponent<Collider2D>().bounds.size.xYour answer
 
 
             Follow this Question
Related Questions
Instantiate Prefab Array with ID 2 Answers
Instantiate an array of prefabs? 0 Answers
Spawning Objects Using An Array. 1 Answer
What is the best way to convert a string list to a class list? 1 Answer
How to deal with for loop and array? 0 Answers
 koobas.hobune.stream
koobas.hobune.stream 
                       
                
                       
			     
			 
                