- Home /
How can I move all prefabs together?
I'm building the game space invaders and I'm having trouble with how to move all the prefabs together. I tried many ways and I was not successful.
Answer by Rugbug_Redfern · Nov 09, 2017 at 12:11 AM
Try this.
NaveI = GameObject.FindGameObjectsWithTag ("NaveInimiga");
foreach( GameObject theEnemy in NaveI ) {
theEnemy.transform.position += new Vector3(2f, 0f, 0f) ;
}
You can't edit transform.position.x, only transform.position. You'll have to set the transform to a new Vector3.
Answer by redrumx · Oct 28, 2017 at 11:36 PM
Have you tried creating an empty object father of all your prefabs? Adding the script to that empty object should work for every child.
I put something like this. I'm building Space invaders game
NaveInimiga is my prefab tag and with this code I returned wrong msg int the line theEnemy.transform.position.x += 2 ; Can not change this value.
I saw this code in the internet and I made some changes.
NaveI = GameObject.FindGameObjectsWithTag ("NaveInimiga"); foreach( GameObject theEnemy in NaveI ) { theEnemy.transform.position.x += 2 ;
}
When you want to write a code it's better if you click on the icon that has "101 010" and copy it there. Otherwise is pretty hard to read it.
Answer by msafa · Nov 09, 2017 at 01:37 AM
If I were you, I would use script. Create it and add as a component for prefab.
public int speed = 5;
void Update () {
transform.position += transform.right * Time.deltaTime * speed;
}
if you have more than one prefab to move. You can seperate with their tag's or name in script.
Example;
if (transform.tag.Equals("Wind"))
{
transform.position += transform.right * Time.deltaTime ;
}else if (transform.name.Equals("Bullet"))
{
transform.position -= transform.right * Time.deltaTime * speed
}
Your answer
![](https://koobas.hobune.stream/wayback/20220612142046im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Emerging Gap when moving my "Snake" 1 Answer
changing the speed of an enemy on key press? 2 Answers
fixed distance for Vector3.moveTowards ? 1 Answer
Bullet Prefab spawning in multiple random positions and rarely the correct one (Unity 3D) 1 Answer
Making something move relative to something else, but slower/shorter ? 2 Answers