Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 Aura_ · Apr 04, 2015 at 08:34 PM · instantiateprefabarrayunity5size

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]);
 
         }
     }
 
 
 
 }
 

alt text

unity.png (10.9 kB)
Comment
Add comment · Show 2
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 Xarbrough · Apr 04, 2015 at 09:42 PM 0
Share

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

avatar image Aura_ · Apr 05, 2015 at 05:20 PM 0
Share
 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?

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by jcv8000 · Apr 04, 2015 at 09:46 PM

Collider2D is a component. On line 28 use

GetComponent<Collider2D>().bounds.size.x
Comment
Add comment · 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

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

4 People are following this question.

avatar image avatar image avatar image avatar image

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


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