random key or gameobject name for destructable saver / position saver
Posted: Sun Jul 11, 2021 4:36 pm
Hi there,
I got the following problem. I instantiate an item from an inventory which has the destructable saver and position saver on itWhen I leave the cube (which already is present in scene 1, so it is not being instantiated and it has named keys, see screenshot) in scene 1 and move from scene 1 to scene 2 to using a portal, and return to scene 1, the cube is where I left it. When I go to scene 2 and instantiate the cube (a prefab with the same components, but with no named keys) from my inventory and leave the cube there, go to scene 1 and back to scene 2, I now have the problem that the cube is gone. I think this is because the prefab does not have named keys assigned to it, causing conflicts with other instantiated cubes. Is there a way to assign random keys after instantiation? Maybe I only need to randomize the name of the cube, since a blanc key takes the gameobject name. But how do you do that after instantiation? I tried using Start(){gameObject.name = "Item" + Random.Range(1, 9999999999999999).ToString() + Random.value.ToString(); } in a script attached on the prefab, but this does not work, probably because Start() is invoked again when re-entering the scene and the name/keys get randomized again... So that would mean that I would have to find a way that the name is only set once... Am I thinking in the right direction? Any thoughts?
Kind regards,
Stefan
I got the following problem. I instantiate an item from an inventory which has the destructable saver and position saver on itWhen I leave the cube (which already is present in scene 1, so it is not being instantiated and it has named keys, see screenshot) in scene 1 and move from scene 1 to scene 2 to using a portal, and return to scene 1, the cube is where I left it. When I go to scene 2 and instantiate the cube (a prefab with the same components, but with no named keys) from my inventory and leave the cube there, go to scene 1 and back to scene 2, I now have the problem that the cube is gone. I think this is because the prefab does not have named keys assigned to it, causing conflicts with other instantiated cubes. Is there a way to assign random keys after instantiation? Maybe I only need to randomize the name of the cube, since a blanc key takes the gameobject name. But how do you do that after instantiation? I tried using Start(){gameObject.name = "Item" + Random.Range(1, 9999999999999999).ToString() + Random.value.ToString(); } in a script attached on the prefab, but this does not work, probably because Start() is invoked again when re-entering the scene and the name/keys get randomized again... So that would mean that I would have to find a way that the name is only set once... Am I thinking in the right direction? Any thoughts?
Kind regards,
Stefan