How to Change Position of a GameObject to another GameObject C#
I am trying to create a game, or simulation rather where you walk around the room and collect cubes. When you "collect" one by walking into it, it will up your score by one and move the pickup to a different location within the room. In order to keep the cube in the room, I randomized the new transform by creating a series of reference points nearby and setting the transform equal to one of those.
// Use this for initialization
void Start ()
{
References.Add(Reference);
References.Add(Reference1);
References.Add(Reference2);
References.Add(Reference3);
References.Add(Reference4);
References.Add(Reference5);
References.Add(Reference6);
References.Add(Reference7);
References.Add(Reference8);
References.Add(Reference9);
References.Add(Reference10);
References.Add(Reference11);
}
// Update is called once per frame
void Update () {
}
void OnTriggerEnter(Collider other)
{
if(other.gameObject.CompareTag("PickUp"))
{
other.gameObject.transform.position = References[Random.Range(0, References.Count)].transform.position;
}
}
}
You'll see, I created a list, added my twelve reference gameObjects to the list, then when you collide with the pickup, it is supposed to transport to another one of them at random but it doesn't. The player just clips through the pickup. Any help with this is appreciated, and I am relatively new with unity so apologies for any mistakes in code formatting, or inefficiencies.
I don't see any problems. First step would be to confirm the code is actually being executed. Stick a Debug.Log("Pickup"); line, inside the if statement, to confirm the code flows as expected. You can also use this command to display various positional values, what object is randomly selected, etc...
Answer by slamancusa · Nov 12, 2016 at 08:05 PM
Hm, thanks for the reply. I'm currently away from my computer until later tommorow but i will be sure to test what you've said. For reference, what if the code is not being executed as expected? Are there any immediate fixes or is it just a matter of troubleshooting? @Glurth