- Home /
Question by
intangibletruth · Sep 05, 2014 at 05:17 AM ·
c#javascript
How to spawn an object after pickup?
Hello, I am new here. I have some code that spawns my prefab ghost in 3 places, which is fine, but it is based off of a timer (i wanted to test it). Now I want to pick them up one after the other until the game ends. Problem is, I am not sure how to do that. Can anyone help me? Thank you
using UnityEngine; using System.Collections;
public class SpawnScript : MonoBehaviour {
public float timer = 0.0f;
public Transform swap1;
public Transform swap2;
public Transform swap3;
public Transform location;
public GameObject go;
public GameObject myPrefab ;
bool ateHim=false;
void Start () {
}
void Update () {
timer += Time.deltaTime;
//Debug.Log(timer);
if(timer >=2)
Swap();
}
/*void OnTriggerEnter(Collider other){
if (!ateHim) {
Swap ();
}
}*/
void Swap()
{
timer = 0;
int randomPick = Mathf.Abs(Random.Range(1,4));
if(randomPick ==1)
{
location = swap1;
Debug.Log("Pos 1");
}
if(randomPick ==2)
{
location = swap2;
Debug.Log("Pos 2");
}
if(randomPick ==3)
{
location = swap3;
Debug.Log("Pos 3");
}
if (!ateHim) {
//do nothing
//go = Instantiate (myPrefab, location.position, location.rotation) as GameObject;
ateHim=true;
}
if(ateHim){
go = Instantiate (myPrefab, location.position, location.rotation) as GameObject;
}
//go.rigidbody.AddForce(location.forward * 100f);
}
}
Comment
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Acces C# via Unityscript 2 Answers
Hide the ImageTraget in Real world 0 Answers
How to make a mesh adjust its form around specific points 1 Answer