Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

void Update depending on where the object spawned

Discussion in 'Scripting' started by ihgyug, Feb 15, 2018.

  1. ihgyug

    ihgyug

    Joined:
    Aug 5, 2017
    Posts:
    194
    Hi all,
    I currently have a script that spawns squares 50/50 either at the right corner or left corner of the camera and randomly across the Y axis (with certain boundaries, set in the in Inspector). This squares have 6 second of "life" because I destroy their clone after 6 seconds each.
    Here the script :
    Code (csharp):
    1.  
    2.     public GameObject enemySquare;
    3.     public int spawnMinY;
    4.     public int spawnMaxY; //max is exclusive!
    5.     public int minusX;
    6.     public int X; //max is exclusive!
    7.     public float respawnTime;
    8.     public float initialDelay;
    9.     public static int randomX;
    10.     private GameObject clone;
    11.  
    12.  
    13.     void Start () {
    14.         InvokeRepeating("SpawnSquares", initialDelay, respawnTime);
    15.         StartCoroutine(Harder());
    16.     }
    17.  
    18.     void SpawnSquares()
    19.     {
    20.         randomX = Random.Range(minusX, X);
    21.         if (randomX < 0)
    22.         {
    23.             GameObject clone = Instantiate(enemySquare, new Vector2(minusX, Random.Range(spawnMinY, spawnMaxY)), Quaternion.identity);
    24.             Destroy(clone,6);
    25.         }
    26.         else if (randomX >= 0)
    27.         {
    28.             GameObject clone = Instantiate(enemySquare, new Vector2(X, Random.Range(spawnMinY, spawnMaxY)), Quaternion.identity);
    29.             Destroy(clone,6);
    30.         }
    31.     }
    32.  
    Now the problems start, I have this other script, which is on the square prefab, called "Rotation".
    This script gives a random color to the spawned square at start, and rotates them randomly but also give a movement to the square. All fine, but I would like the movement to respect the position where the square spawned.
    For example, if the square spawned at right corner of the camera, it should move to the left, and viceversa.
    As now the script moves all squares together and not independently, which results in weird movements mid-air. That's because my Update check every frame if a square spawned right and left and moves all squares to the same direction.
    Here the script :
    Code (csharp):
    1.  
    2.    int randomUpdate;
    3.  
    4.     public void Start()
    5.     {
    6.         randomUpdate = Random.Range(1, 11);
    7.         gameObject.GetComponent<SpriteRenderer>().color = new Color(Random.Range(0.0f, 1.0f), Random.Range(0.0f, 1.0f), Random.Range(0.0f, 1.0f));
    8.         InvokeRepeating("Rotate", 0, 0.8f);
    9.     }
    10.     public void Rotate()
    11.     {
    12.         if (randomUpdate <= 5)
    13.         gameObject.transform.Rotate(new Vector3(0, 0, -45));
    14.         if (randomUpdate > 5)
    15.         gameObject.transform.Rotate(new Vector3(0, 0, 45));
    16.     }
    17.     public void Update()
    18.     {
    19.        if (Spawner.randomX >= 0)
    20.           gameObject.transform.Translate(-0.08f, 0, 0, Space.World);
    21.        if (Spawner.randomX < 0)
    22.            gameObject.transform.Translate(0.08f, 0, 0, Space.World);
    23.     }
    24. }
    25.  
    26.  
    The desired effect would be to have each square move left or right depending on where they spawned, independently from other squares.
    Any help/advice is greatly appreciated.
    Thank you all.
     
  2. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,186
    Instead of checking your Spawner.randomX, you should set a value on the square that tells if it spawned on the right or left and then move the square based on that value. Set this when you instantiate it. Then your if checks will check that value and each one can move based on their own stored value. Otherwise, they are each looking at the same stored value on your spawner, so they will all respond the same.

    There are other things to look into. A manager class that handles a single Update and moves all squares. You may also want to learn about object pooling, but these two points are for optimization and aren't important for your issue.
     
  3. ihgyug

    ihgyug

    Joined:
    Aug 5, 2017
    Posts:
    194
    Maybe I am doing this wrong...because if I set a bool to check if left or right, everytime a square is instanciated it will change this value making every square change direction.
    The Update will check if this value changes every frame and the squares will move in mid-air randomly left and right...
     
  4. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,186
    I may not understand your intention.

    1. Spawn new square
    2. Set a variable on the square to track if it spawned on the left or right (this is done once)
    3. Update on the square starts to move the square depending on the value of the variable.

    That is what I thought you were trying to do. Am I misunderstanding? Note the variable is on the square, not on your spawner. Each square has it's own variable that must be set to track where it spawned at. You can't use a single variable on your spawner to check against. This is the current issue you have.
     
  5. ihgyug

    ihgyug

    Joined:
    Aug 5, 2017
    Posts:
    194
    My bad, I was doing some confusion.
    I corrected the script as you suggested and now works perfectly.
    Thank you for helping ;)
     
  6. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,186
    Glad you got it working. :)