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 Sefean · Nov 18, 2016 at 11:57 AM · prefabscript.sceneslevels

Problems editing a level from script

Hello, I'm trying to create levels dynamically. To do so, I have created a scene and I edit the ammount of components I have in the scene using a script. Here's the code I'm using:

  public class BuildScene : MonoBehaviour
 {
 public Transform square;

 void Start()
 {
     int numFilas = ApplicationModel.numberRows;
     int numObj = 3;

     float posY = 2.5f;
     for (int i = 0; i < numFilas; i++)
     {
         for (float j = 0; j < numObj; j++)
         {
             float posX = -0.9f;
             Instantiate(square);
             
             square.transform.localScale = new Vector3(0.6f, 0.6f, 0.1f);
             square.transform.position = new Vector3(posX+j, posY-i, 0);
         }
     }
 }

Public Transform square is a prefab of a 3dcube with a material.

numberRows changes deppending on the button you press (level 1 = 1 row, level 2 = 2 rows, etc.).

The problem is that the first time you enter a level the last cube of the last row doesn't appear, or is in a wrong position, but the second time you enter into that level it works perfectly. Any ideas?

If you want I pass you the project (is really small right now) so you can execute it.

Comment
Add comment
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

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by Landern · Nov 18, 2016 at 02:28 PM

You need to use the instantiated square's new reference instead of the one you're basically copying from the inspector.

Change your Start method to use a local variable that is a reference to the new game object. This will also lessen the complexity of the code a bit since you're already dealing with a transform which you where before... anywho

  void Start()
  {
      int numFilas = ApplicationModel.numberRows;
      int numObj = 3;
      float posY = 2.5f;
      for (int i = 0; i < numFilas; i++)
      {
          for (float j = 0; j < numObj; j++)
          {
              float posX = -0.9f;
              Transform newSquare = Instantiate(square);
              
              newSquare.localScale = new Vector3(0.6f, 0.6f, 0.1f);
              newSquare.position = new Vector3(posX+j, posY-i, 0);
          }
      }
  }

Now stuff looks like (1 row):

One Row

And now 6 rows:

Six Rows


sixrows.png (1.7 kB)
onerow.png (964 B)
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 Sefean · Nov 18, 2016 at 03:29 PM 0
Share

Thanks a lot, I'll try it when I get home.

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

remembering variables between scenes 2 Answers

How to keep my Prefab from Restiing 0 Answers

Objects disappearing and scripts not working in 2 scenes. Corrupt scenes? 1 Answer

I'm trying to procedurally generate a couple prefabs for a game i'm making in 3D 2 Answers

Save GameObject to file without Playerpref 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