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 Masherak · Jan 03, 2015 at 06:05 PM · instantiateposition

How set position of spawning clones

Please help, how I can set spawning position (now is: 0, 0, 0) for below inscribed code.

I walked through the tutorials but without result.

Everything what I trying causing crash of structure Xn x Yn.

Thank you very much

 using UnityEngine;
 using System.Collections;
 
 public class Spawn : MonoBehaviour {
 
     public GameObject Prefab;
     public float timer = 0f;
     public float time_of_spawn = 0f;
     public int Xn;
     public int Yn;
 
 
 
     
     void Update() {
 
 
         timer += Time.deltaTime;
 
 
 
         if (timer > time_of_spawn) 
         {
         
     
 
             for (int x = 0; x < Xn; x++)
             {
                 for (int y = 0; y < Yn; y++)
                 {
                 
                 
                 Instantiate(Prefab, new Vector3 (x, y, 0f), Quaternion.identity);
 
 
 
             timer = 0f;
 
     
 
         
                 }
             }
     }
     }
 }
Comment
Add comment · Show 4
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 Mmmpies · Jan 03, 2015 at 06:46 PM 0
Share

You've not set Xn or Yn to anything are you setting that in the inspector and if so what are you setting it to?

avatar image Masherak · Jan 03, 2015 at 07:02 PM 0
Share

If I set Xn: 8 and Yn: 8 so in game I make 8x8 ( 64 x prefab )in regular square ( I don't want scattered ), and I need - How I can set position for this square ? I must create next script ?

avatar image Mmmpies · Jan 03, 2015 at 07:05 PM 0
Share

O$$anonymous$$ so it's not just that you haven't set them. Are you getting an error message?

avatar image Masherak · Jan 03, 2015 at 07:13 PM 0
Share

Above-mentioned code running without error. But I dont know how set position of this spawning. For example (0f, 6f, 0f) ins$$anonymous$$d of default (0f, 0f, 0f).

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Mmmpies · Jan 03, 2015 at 07:22 PM

Well your code is almost there, I'm assuming this is a 3D game as you're using a vector 3 let me know if not.

I edited your code, the timer thing was just looping and creating lots of instances so I just put a bool in to spawn your 64 objects then stop.

 using UnityEngine;
 using System.Collections;
 
 public class Spawn: MonoBehaviour {
     
     public GameObject Prefab;
     public float timer = 0f;
     public float time_of_spawn = 0f;
     public int Xn;
     public int Yn;
 
     private bool spawn = true;
     
     void Update() {
         //timer += Time.deltaTime;
 
         if (spawn) 
         {
             spawn = false;
             for (int x = 0; x < Xn; x++)
             {
                 for (int y = 0; y < Yn; y++)
                 {
                     Instantiate(Prefab, new Vector3 (100 + x,  0.5f, 100 + y), Quaternion.identity);
                     //timer = 0f;
                 }
             }
         }
     }
 }

You also had y being used which in 3D is upwards, I didn't change the name just move it to the Z position.

I also instantiated (cubes in my test) so I set the Y value to .5f so it didn't drop through the terrain.

Finally I added 100 to X and Z so it was away from the edge of the map but should still work for you if you want them to start from 0 0 0.

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 Masherak · Jan 05, 2015 at 05:42 PM 0
Share

Thank you, you helped me too much... :-)

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Instatiating a prefab in a random position 0 Answers

How can I get the x position for the left(and right) of the screen? 2 Answers

Make object move in a direction depending on where it spawns? (C#) 1 Answer

Help making a damage number system 1 Answer

Position + Vector3 doesn't return correct values 1 Answer


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