Invocar

Tutorial

Beginner

+0XP

5 mins

147

Unity Technologies

Invocar

Las funciones Invoke (invocar) te permiten programar invocaciones de métodos para que ocurran en un momento determinado. En este video aprenderás cómo usar las funciones Invoke, InvokeRepeating y CancelInvoke en tus scripts de Unity.

Este tutorial está incluido en el proyecto Scripting para principiantes.

Previo: Ejemplificar

Siguiente: Arrays

1. Invocar

InvokeScript

using UnityEngine;
using System.Collections;

public class InvokeScript : MonoBehaviour 
{
    public GameObject target;
    
    
    void Start()
    {
        Invoke ("SpawnObject", 2);
    }
    
    void SpawnObject()
    {
        Instantiate(target, new Vector3(0, 2, 0), Quaternion.identity);
    }
}

InvokeRepeating

using UnityEngine;
using System.Collections;

public class InvokeRepeating : MonoBehaviour 
{
    public GameObject target;
    
    
    void Start()
    {
        InvokeRepeating("SpawnObject", 2, 1);
    }
    
    void SpawnObject()
    {
        float x = Random.Range(-2.0f, 2.0f);
        float z = Random.Range(-2.0f, 2.0f);
        Instantiate(target, new Vector3(x, 2, z), Quaternion.identity);
    }
}

Complete this Tutorial