Answers for "instantiate script"

C#
3

instantiate script

// Instantiates 10 copies of Prefab each 2 units apart from each otherusing UnityEngine;
using System.Collections;public class ExampleClass : MonoBehaviour
{
    public Transform prefab;
    void Start()
    {
        for (int i = 0; i < 10; i++)
        {
            Instantiate(prefab, new Vector3(i * 2.0F, 0, 0), Quaternion.identity);
        }
    }
}
Posted by: Guest on March-25-2020
0

instantiate script

using UnityEngine;// Instantiate a rigidbody then set the velocitypublic class Example : MonoBehaviour
{
    // Assign a Rigidbody component in the inspector to instantiate    public Rigidbody projectile;    void Update()
    {
        // Ctrl was pressed, launch a projectile
        if (Input.GetButtonDown("Fire1"))
        {
            // Instantiate the projectile at the position and rotation of this transform
            Rigidbody clone;
            clone = Instantiate(projectile, transform.position, transform.rotation);            // Give the cloned object an initial velocity along the current
            // object's Z axis
            clone.velocity = transform.TransformDirection(Vector3.forward * 10);
        }
    }
}
Posted by: Guest on March-27-2020
0

instantiate script

using UnityEngine;
using System.Collections;public class Missile : MonoBehaviour
{
    public int timeoutDestructor;    // ...other code...
}
public class ExampleClass : MonoBehaviour
{
    // Instantiate a Prefab with an attached Missile script
    public Missile projectile;    void Update()
    {
        // Ctrl was pressed, launch a projectile
        if (Input.GetButtonDown("Fire1"))
        {
            // Instantiate the projectile at the position and rotation of this transform
            Missile clone = Instantiate(projectile, transform.position, transform.rotation);            // Set the missiles timeout destructor to 5
            clone.timeoutDestructor = 5;
        }
    }
}
Posted by: Guest on June-08-2021

C# Answers by Framework

Browse Popular Code Answers by Language