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 Axelofthekey · Mar 08, 2015 at 12:10 PM · instantiateprefabscalelists

Changing LocalScale in code affecting prefab stored in Assets

As a part of some basic code I'm writing, I make a copy of a prefab as a member of a list. I then edit the localScale of the object in the list. When I do this however, the scale of the object changes in my assets windows and persists after the game stops running. Here is my code:

 using UnityEngine;
 using System.Collections;
 using System.Collections.Generic;
 
 public class CreatePlatforms : MonoBehaviour 
 {
     public GameObject platformPrefab;
     private int numToMake;
     private List<GameObject> platforms = new List<GameObject>();
 
     // Use this for initialization
     void Start () 
     {
         numToMake = 2;
         Create();
     }
     
     // Update is called once per frame
     void Update () 
     {
     
     }
 
     void Create()
     {
         for(int i = 0; i < numToMake; i++)
         {
             platforms.Add(platformPrefab);
             platforms[i].transform.localScale /= numToMake;
             Instantiate(platforms[i], new Vector3(1.2f + numToMake*1.2f, .85f + numToMake*.85f, 0), Quaternion.identity);
         }
     }
 }
 

The "platformPrefab" is attached to my script through the editor.

I also realize the math on my Instantiate command won't do what I want for a variety of reasons, but the localScale problem is more an issue to me at the moment.

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
0

Answer by HammerCar · Mar 13, 2015 at 06:24 PM

You are actually changing the scale of the prefab in the project window. To only change the scale of the GameObject you have just Instantiated you can do this:

 void Create()
 {
      for(int i = 0; i < numToMake; i++)
      {
          platforms.Add(platformPrefab);
          GameObject platform = (GameObject)Instantiate(platforms[i], new Vector3(1.2f + numToMake*1.2f, .85f + numToMake*.85f, 0), Quaternion.identity); // You have have to cast it to a GameObject
          platform.transform.localScale /= numToMake;
      }
 }

So now you only effect the GameObject you have just Instantiated.

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

When I instantiate a 2D bullet it's BoxCollider2D fails verification. 0 Answers

Instantiating Cube Prefabs with scale 3 Answers

Get scale of instantiated prefab and resize 1 Answer

Do I necessarily need to instantiate a prefab before putting it in List to conserve his variable data? 0 Answers

Instantiated prefab scale is wrong. 2 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