- Home /
 
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.
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.
Your answer