1

I'm making a 'run' game in Unity and i'm making a prototype with a ball, that has other balls following him. If the followers hit an object they get destroyed after some time. I made a kind of road with obstacles on them and prefabt them. Now is my question how can i load in a prefab on trigger or something so my game keeps going. The goal is that i dont need to make a whole map but that the map is randomly chosen from prefabs of different roads.

If it would help this is the code for my main character, i tought maybe its in another trigger that you load a prefab or something?

This is my code.

using UnityEngine;
using System.Collections;

public class Ball : MonoBehaviour {

public float InputForce;
public GUIText guiText;
public float rotationHorizontal;
public AudioClip ACeffect2;
public GameObject zombiePrefab;

void FixedUpdate() {

    rigidbody.AddForce( Camera.main.transform.right * Input.GetAxis("Horizontal") * InputForce);
    rigidbody.AddForce( Camera.main.transform.forward * Input.GetAxis("Vertical") * InputForce);

    transform.position += Vector3.forward *InputForce * Time.deltaTime;
    rotationHorizontal = Input.GetAxis("Horizontal") * InputForce;
    rotationHorizontal *= Time.deltaTime;
    rigidbody.AddRelativeTorque (Vector3.back * rotationHorizontal);

}

void OnCollisionEnter(Collision col){
    if (col.gameObject.name == "Zombie") {
        Debug.Log ("Player geraakt, nu ben je eigenlijk dood");
    }
    if (col.gameObject.name == "Obstakel1") {
        Debug.Log ("Obstakel1 geraakt");
        audio.PlayOneShot(ACeffect2);
        InputForce = 0;
    }
    if (col.gameObject.name == "Obstakel2") {
        Debug.Log ("Obstakel2 geraakt");
    }
}

void AddZombies(int aantal){
    for (int i = 0; i < aantal; i++){
        GameObject go = GameObject.Instantiate(zombiePrefab, transform.position - new Vector3(0, 0, 7 + i),Quaternion.identity) as GameObject;
        Zombie zb = go.GetComponent<Zombie>();
        zb.target = gameObject.transform;
    }
}

void OnTriggerEnter(Collider col) {
    Debug.Log ("Enter" +col.name);
    if (col.tag == "AddZombies"){
        AddZombies(4);
    }
}

void OnTriggerExit(Collider col) {
    Debug.Log ("Leaving with" +col.name);
}
}
Andy
  • 8,432
  • 6
  • 38
  • 76
Pixelsquare
  • 173
  • 2
  • 14
  • possible duplicate of [Add gameobject dynamically to scene in Unity3d](http://stackoverflow.com/questions/15500137/add-gameobject-dynamically-to-scene-in-unity3d) – apxcode Jan 09 '15 at 14:47

1 Answers1

3

Make a gameobject and add a component containing the following code:

public GameObject prefab; // Drag and drop prefab to component in unity

// When trigger is triggered
void OnTriggerEnter(Collider col) 
{
    Instantiate(prefab, new Vector3(0,0,0), Quaternion.Identity);
}

You can then drag and drop the prefab you want to instantiate to the component in unity. The position could be where ever you want it to spawn (for example transform.position to make it spawn where you have placed your trigger).

Note that to make a trigger you need to make a new empty gameobject and add what ever collider you want to have on it. To make it call the OnTriggerEnter method you also need to check the box is trigger on the collider component.

Simon Karlsson
  • 4,090
  • 22
  • 39