How do i add my objects to a tag
I've made this "frogger" game with 8 cars, but i want to have an "up" and a "down" tag so i dont have to make a startcar1-100 for every car. I already made the tags but i dont know how to add the objects to the tag
using UnityEngine;
using System.Collections;
public class scriptCar : MonoBehaviour {
public GameObject up, down, startCar1, startCar2, startCar3, startCar4, startCar5, startCar6, startCar7, startCar8;
public float speed1, speed2, speed3, speed4, speed5, speed6, speed7, speed8;
public int score;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
speed1 = Random.Range (5, 10);
speed2 = Random.Range (7, 12);
speed3 = Random.Range (1, 6);
speed4 = Random.Range (9, 14);
speed5 = Random.Range (4, 9);
speed6 = Random.Range (6, 1);
speed7 = Random.Range (7, 12);
speed8 = Random.Range (3, 9);
startCar1.transform.Translate (0, -speed2 * Time.deltaTime, 0);
if (startCar1.transform.position.y < -9){
startCar1.transform.position = new Vector3 (-2.3F, 8, 0);
}
startCar2.transform.Translate (0, -speed2 * Time.deltaTime, 0);
if (startCar2.transform.position.y < -9){
startCar2.transform.position = new Vector3 (-1.65F, 8, 0);
}
startCar3.transform.Translate (0, -speed3 * Time.deltaTime, 0);
if (startCar3.transform.position.y < -9){
startCar3.transform.position = new Vector3 (-1.05F, 8, 0);
}
startCar4.transform.Translate (0, -speed4 * Time.deltaTime, 0);
if (startCar4.transform.position.y < -9){
startCar4.transform.position = new Vector3 (-0.35F, 8, 0);
}
startCar5.transform.Translate (0, speed5 * Time.deltaTime, 0);
if (startCar5.transform.position.y > 9){
startCar5.transform.position = new Vector3 (0.35F, -8, 0);
}
startCar6.transform.Translate (0, speed6 * Time.deltaTime, 0);
if (startCar6.transform.position.y > 9){
startCar6.transform.position = new Vector3 (1F, -8, 0);
}
startCar7.transform.Translate (0, speed7 * Time.deltaTime, 0);
if (startCar7.transform.position.y > 9){
startCar7.transform.position = new Vector3 (1.65F, -8, 0);
}
startCar8.transform.Translate (0, speed8 * Time.deltaTime, 0);
if (startCar8.transform.position.y > 9){
startCar8.transform.position = new Vector3 (2.25F, -8, 0);
}
}
}
you should not do it like this. ever.
use an array and loops, you should look into $$anonymous$$athf.Repeat(). and the script should not handle each car, ins$$anonymous$$d each car should use this script, and the script should work for each car
btw: error in line 27 you never use speed1
But that cant work because i have 8 lines of road, each car has to go on a differnt road, if i make the script for all cars all cars would go on the same road?
Answer by Jessespike · Dec 16, 2015 at 07:52 PM
public GameObject startCar1;
void Start() {
startCar1.tag = "Put_Tag_Name_Here";
}
Your answer
Follow this Question
Related Questions
How to compare audio sequences in unity 0 Answers
tagged objects problem 0 Answers
Multiple Tags with FindGameObjectsWithTag...? 1 Answer
Why ain't this working? 1 Answer
I want to do a pause panel 1 Answer