Search Unity

call the code through the last animation

Discussion in 'Testing & Automation' started by cd9992107, Sep 24, 2019.

  1. cd9992107

    cd9992107

    Joined:
    Aug 9, 2019
    Posts:
    14
    well i need help if you can answer i will be grateful. my object is falling, and when it arrives at some point it reappears somewhere else random, I'm using this code, all right.
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using System.Collections.Generic;
    4.  
    5. public class SpawnController : MonoBehaviour {
    6.  
    7.     public float maxHeight;    
    8.     public float minHeight;    
    9.  
    10.     public float rateSpawn;    
    11.     private float currentRateSpawn;
    12.  
    13.     public GameObject objectPrefab;     .
    14.     public int maxSpawnObjects;    
    15.     private List<GameObject> objectsList = new List<GameObject>();  
    16.  
    17. // Use this for initialization
    18. void Start () {
    19.  
    20.         for (int i = 0; i < maxSpawnObjects; i++) {    
    21.            
    22.             objectsList.Add(Instantiate(objectPrefab));    
    23.             objectsList[i].SetActive(false);    
    24.         }
    25.     }
    26. // Update is called once per frame
    27. void Update () {
    28.  
    29.         currentRateSpawn += Time.deltaTime;  
    30.         if (currentRateSpawn >= rateSpawn) {    
    31.  
    32.             currentRateSpawn = 0;
    33.             Spawn();  
    34.  
    35.  
    36.         }
    37.  
    38.  
    39.    
    40. }
    41.  
    42.     private void Spawn() {  
    43.  
    44.         float randHeight = Random.RandomRange(maxHeight, minHeight);  
    45.  
    46.         for (int i = 0; i < maxSpawnObjects; i++) {    
    47.  
    48.             if (objectsList[i].activeSelf == false) {      .
    49.  
    50.                 objectsList[i].transform.position = new Vector3(transform.position.x, randHeight, 0);  
    51.                 objectsList[i].SetActive(true);  
    52.                 break;
    53.             }
    54.         }
    55.     }
    56. }


    but I need to change this script.

    when i use my last animation i want to call a script call the random reappearance of my object, just that line of script.
     
  2. Kaiymu_

    Kaiymu_

    Joined:
    Jun 1, 2016
    Posts:
    47
    Hello,

    Not sure this is the right place, but in the animation window, you can add custom callbacks whenever you want
    https://docs.unity3d.com/540/Documentation/Manual/animeditor-AnimationEvents.html

    The documentation seems depracated, but the idea is the same. Add a small animation event by using the small white pen that you see. And let it call your piece of code :).
    The animation AS to be on an object that contains the script, otherwise, it's not going to work.

    Have a good day.