Search Unity

Why instead of the scene sprites moving, the...

Discussion in '2D' started by Amon, Nov 18, 2013.

  1. Amon

    Amon

    Joined:
    Oct 18, 2009
    Posts:
    1,384
    updates are applied to the base sprite in the project view?

    Basically, I import my sprite, I drag it to the Hierarchy/Scene window which automatically adds a Transform and SpriteRenderer, then drag it back to the Project window which automagically makes it in to a prefab. I then create an empty GameObject, attach a script that references the prefab and in the Inspector I link it up.

    Now when I try to manipulate the Transform of the prefab, instead of it updating the sprite prefab in the scene, it updates the prefab in the project window.....

    Why?
     
  2. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    That's how Unity has always worked. If you're referencing the prefab, then anything you do to that reference will affect the prefab. If you only want to affect instances of the prefab, then you need to reference an instance instead of the prefab itself.

    --Eric
     
  3. Amon

    Amon

    Joined:
    Oct 18, 2009
    Posts:
    1,384
    Ahh! That's something I forgot. It's been a while since I have done any programming.

    iirc when I Instantiate the prefab, I should Instantiate clones instead by either using a class and list to store individual clones and their values?
     
  4. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    Not quite sure what you mean...generally you do something like this:

    Code (csharp):
    1. var prefab : GameObject;
    2.  
    3. function Start () {
    4.      var clone = Instantiate (prefab);
    5.      clone.position.x = 100;
    6. }
    --Eric
     
  5. Amon

    Amon

    Joined:
    Oct 18, 2009
    Posts:
    1,384
    I managed to come up with something as a test. Not sure if it is overkill but it'll do for now. I have attached the Unity Package for anyone stuck.

    The package contains the Player Sprite, the dudeClass file, playerscript file, scene etc.

    Here is the Dude Class:

    Code (csharp):
    1.  
    2. #pragma strict
    3.  
    4.  
    5. public class Dude {
    6.    
    7.     public var dudeClone : GameObject;
    8.     public var rNum : int;
    9.    
    10.     public function Dude ( dc : GameObject,  xl : float, yl : float ) {
    11.         dudeClone = dc;
    12.         dudeClone.transform.position.x = xl;
    13.         dudeClone.transform.position.y = yl;
    14.         rNum = Random.Range(0.1, 1.0);
    15.     }
    16.    
    17.     public function moveDude () {
    18.         dudeClone.transform.position.y -= 4 * Time.deltaTime;
    19.         if ( dudeClone.transform.position.y < -6 ) {
    20.             dudeClone.transform.position.y = 7;
    21.         }
    22.     }
    23.    
    24. }
    25.  
    26.  
    27.  
    Here is the Player Script:

    Code (csharp):
    1.  
    2. #pragma strict
    3. import System.Random;
    4. import System.Collections.Generic;
    5. import System.Collections.IEnumerable;
    6.  
    7.  
    8.  
    9.  
    10.  
    11.  
    12. var dudeSprite : GameObject;
    13.  
    14.  
    15. @HideInInspector
    16. public var dude:Dude;
    17. @HideInInspector
    18. public var list:List.<Dude>;
    19.  
    20.  
    21. function Start () {
    22.    
    23.     if ( list == null ) {
    24.         list = new List.<Dude>();
    25.     }
    26.    
    27.     for ( var xiter : int = 0; xiter < 20; xiter++ ) {
    28.         dude = new Dude( Instantiate(dudeSprite), Random.Range(-6, 6), Random.Range( -6, 6));
    29.         list.Add(dude);
    30.     }                      
    31.                                        
    32. }
    33.  
    34.  
    35. function Update () {
    36.     for ( dude in list ) {
    37.         dude.moveDude();
    38.     }
    39. }
    40.  
     

    Attached Files: