- Home /
How do I update the position of different prefabs to the current position?
Hello everyone,
I'm making a game where the player is a square and it changes shape when he presses a key. To do that I made different prefabs because I have different things to change. When I press the key to change to one prefab, I set active the corresponding prefab and I disable the other. The problem is here, I have three prefabs and I don't know where to connect to change the position runtime. I tried to make the position equal to the others but it didn't work. Here there is the code I made:
using UnityEngine;
public class Transformation : MonoBehaviour
{
public GameObject normal, small, big;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
if (Input.GetKeyDown("q"))
{
big.SetActive(true);
normal.SetActive(false);
small.SetActive(false);
big.transform.position = normal.transform.position = small.transform.position;
}
if (Input.GetKeyDown("w"))
{
normal.SetActive(true);
big.SetActive(false);
small.SetActive(false);
normal.transform.position = big.transform.position = small.transform.position;
}
if (Input.GetKeyDown("e"))
{
small.SetActive(true);
normal.SetActive(false);
big.SetActive(false);
small.transform.position = big.transform.position = normal.transform.position;
}
}
}
Answer by LOSTSOUL86 · May 24 at 05:26 PM
Hi I would recommend a slightly different approach. As I understand all prefabs should be in the same position.
You should do 1 object (or 1 prefab) called for example CharacterObject. And put your 3 small, big and normal inside.
Then when you changing position just change the position of CharacterObject. And don't move prefabs individually.
In this way your 3 prefabs will be always aligned in in same way relative to each other.
Let me know if this helps.
Answer by daX-_-_908 · May 24 at 07:32 PM
Hi @LOSTSOUL86, but the problem is that I have three different player controller, one for each prefab. So If I do that I need to put all the different player controller in the same game object and activate only one of them when needed. How do I set active a component? Using "on enable"?
I don't see the whole project. Yes it can be by component.enabled. Also your code can work but to fix you need one thing. Cause you should copy position always from the active gameObject to the one that is being activated. For example the key "e" change state to small. But to assign the position you should check first which state is active. Before this code small.SetActive(true); normal.SetActive(false); big.SetActive(false); Put: if(normal.activeSelf) small.transtorm = normal.transform; else small.transform = big.transform; Same for other states. You need to copy from the last active state not from any state.
It works thank you. I have added the part of "if(normal.activeself)" and it works. Just another question, how do I prevent the set active of the same object when I press the key to activate it after it is already active in the scene?
Just for example if you press "e" if (Input.GetKeyDown("e")) { if(!small.selfActive) { The rest of the code will execute only if the state is not active with negation "!" } }
If the small is active then you do nothing. Only if it's not active. You change it into small.
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
How do I update the position of different prefabs to the current position? 0 Answers
How do I update the position of different prefabs to the current position? 0 Answers
How do I update the position of different prefabs to the current position? 0 Answers
Do you know the solution of this probelm? (Problem is in the details) 0 Answers