How To Make A Kill Counter
So first of all I want to say that I actually suck and dont know any of the basics of coding or unity and this is for school (no time to research basics) and I was wodnering if you guys could give me a step by step break it down in the least technical language
So I have a game that has a bunch of cubes that come from a spawn point and they blow up at a certain proximity and I wanted to make a counter that showed how many cubes exploded. the cubes also run away from you.
Here is the code:
#pragma strict
var theplayer : GameObject;
var speed : float;
var range : int;
var explosion : GameObject;
function Start () {
}
function Update () {
range=Vector3.Distance(theplayer.transform.position,transform.position);
if(range<40){
transform.LookAt(theplayer.transform.position);
}
if(range<30 && range>10){
transform.Translate(Vector3.forward*speed);
}
if(range<12){
transform.Translate(-Vector3.forward*speed);
}
if(range<5){
Instantiate(explosion,transform.position,transform.rotation);
Destroy(gameObject);
}
}
Answer by ronaldovelo · May 05, 2017 at 03:59 PM
Create a GameObject called Counter, and script like this also called Counter
var counter = 0;
function Start () {
}
function Update () {
}
//I think you could delete Start() and Update() methods
and in your script add this line, just before "Destroy(gameObject);",
#pragma strict
var theplayer : GameObject;
var speed : float;
var range : int;
var explosion : GameObject;
function Start () {
}
function Update () {
range=Vector3.Distance(theplayer.transform.position,transform.position);
if(range<40){
transform.LookAt(theplayer.transform.position);
}
if(range<30 && range>10){
transform.Translate(Vector3.forward*speed);
}
if(range<12){
transform.Translate(-Vector3.forward*speed);
}
if(range<5){
Instantiate(explosion,transform.position,transform.rotation);
var go = GameObject.Find("Counter").GetComponent(Counter).counter++;
Destroy(gameObject);
}
}
I think this is it, test it and feedback
Your answer
Follow this Question
Related Questions
Do you need to have specific script types for different mobile platforms? 1 Answer
using UnityEngine.SceneManagement: How to use, etc? 1 Answer
Where to start to develop a cross platform App that can be used in Windows, Android, and Apple? 0 Answers
JavaScript taken from the web isn't working. Help 0 Answers
Expecting }, found 'static' 2 Answers