- Home /
transform all GameObjects with the tags("..") C#
I am struggling with my code and I can't find why. I want to move all object (with the tag RED,BLUE or GREEN) in the scene to the right. This action will happen when the left mousebutton is pressed.
the Objects in the scenes do have the tags RED,BLUE or GREEN, one of these attached to them. I have tried to use GameObject.FindGameObjectWithTag("Desired Tag")
But this would only Move 1 object with the tags. Also if GameObject.FindGameObjectWithTag("RED").transform.Translate(Direction*movespeed);
is called. It wont call the other 2 line of code with BLUE and GREEN (Line 21 and 22).
I know the code beneath is wrong, because I should have written FindGameObjectsWithTag("RED") to FindGameObjectsWithTag("RED")[5]. But what I have understand is that the number at the end will only target the 5th object with the tag: "RED".
Another thing I tried is, instead of Void Update... -> Void OnMouseDown(){ FindGameObjectWithTag("RED").transform.Translate(Direction*movespeed); FindGameObjectWithTag("GREEN")tansform.Translate(Direction*movespeed); etc.. }
But I get the same results.
The Objects with the tags will be Instantiated by another script. but I don't that is relevant for this problem.
using UnityEngine;
using System.Collections;
public class OGMovement : MonoBehaviour {
public int movespeed;
private Vector3 Direction;
// Use this for initialization
void Start () {
movespeed = 1;
Direction = Vector3.right;
}
// Update is called once per frame
void Update () {
if (Input.GetMouseButtonDown (0)) {
GameObject.FindGameObjectsWithTag("RED").transform.Translate (Direction * movespeed);
GameObject.FindGameObjectsWithTag("BLUE").transform.Translate (Direction * movespeed);
GameObject.FindGameObjectsWithTag("GREEN").transform.Translate (Direction * movespeed);
}
}
}
Sorry for the long text, but I wanted to explain everything. Thx
-Hexer
Answer by hbalint1 · May 01, 2015 at 01:27 PM
using UnityEngine;
using System.Collections;
public class OGMovement : MonoBehaviour {
public int movespeed;
private Vector3 Direction;
private GameObject[] reds;
private GameObject[] greens;
private GameObject[] blues;
// Use this for initialization
void Start () {
movespeed = 5; // i increased this to make it easier to see
Direction = Vector3.right;
}
// Update is called once per frame
void Update () {
if (Input.GetMouseButtonDown (0)) {
reds = GameObject.FindGameObjectsWithTag("RED");
foreach(GameObject r in reds)
r.transform.Translate (Direction * movespeed * Time.deltaTime);
greens = GameObject.FindGameObjectsWithTag("GREEN");
foreach(GameObject g in greens)
g.transform.Translate (Direction * movespeed * Time.deltaTime);
blues = GameObject.FindGameObjectsWithTag("BLUE");
foreach(GameObject b in blues)
b.transform.Translate (Direction * movespeed * Time.deltaTime);
}
}
}
The code is untested, but I think it should work.
thanks, I also already tried it like you did but than without the foreach(GameObject r in reds), But everytime I did that I got an error that I could not use GameObject.FindGameObjectWithTag(".."); (Yes I did define all variables)
When using the script I encountered some glitches. Not knowing if it was intentional by the engine but the spacing for each object was off. In some occasion I had 3 objects colliding with eachother. I fixed this by using FixedUpdate ins$$anonymous$$d of Update or On$$anonymous$$ouseDown. It fixed it for 90%, in which sometimes the spacing was off, but it never collide with eachother so I was satisfied with it.
Thanks for your help. -Hexer
As I wrote my comment a second ago and googled something about FixedUpdate. apparently with fixedupdate you can call multiple things at the sametime, meaning if you click fast enough you can move the object 2 times as far than normal.
This is an "answer" for your first comment: that's because the GameObject.FindGameObjectsWithTag("RED");
gives back an array of GameObjects. And you can't move an array. you can move GameObjects individually. And you've done it just right. any transformation on Rigidbodies should be in FixedUpdate().
Thanks! r.transform.Translate (Direction movespeed Time.deltaTime); Didn't know the "r" was necessary
Your answer
![](https://koobas.hobune.stream/wayback/20220613194518im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Not finding closest Object with tag player. 2 Answers
Distribute terrain in zones 3 Answers
Find a gameobject with the same position 2 Answers
Multiple Cars not working 1 Answer