prefabs are not going towards the 'trigger'
I made a script to have prefabs walk towards a 'trigger' but they don't walk towards it and instead they stick to the far left then fall through the floor. I don't know whats happening but i think the prefabs can't detect the 'trigger'. please help
using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI;
public class CubeControl : MonoBehaviour {
public Transform target; public float speed; private int current;
public int count; public Text countText; public Text losetext; private Rigidbody rb;
// Use this for initialization
void Start()
{
rb = GetComponent<Rigidbody>();
count = 0;
SetCountText();
losetext.text = "";
}
void OnTriggerEnter(Collider other)
{
if (other.GetComponent<Collider>().tag == "trigger")
{
Destroy(gameObject);
count = count + 1;
SetCountText();
}
}
// Update is called once per frame
void Update()
{
// The step size is equal to speed times frame time.
float step = speed * Time.deltaTime;
// Move our position a step closer to the target.
transform.position = Vector3.MoveTowards(transform.position, target.position, step);
}
void SetCountText()
{ countText.text = "Count: " + count.ToString(); if (count >= 1) { losetext.text = "Game Over!"; }
}
}
Your answer
Follow this Question
Related Questions
Only spawning power ups that the player wants in that game 1 Answer
Problems with instantiating 1 Answer
Can I Create a Prefab in Unity 2018.3.5f1 With C# Script Using a YAML File? 0 Answers
How do i take a grab of clone from prefab and change his velocity 2 Answers
How to interact with multiple instances of a single prefab? 1 Answer