Select Object And Spawn it
Hi, I have a problem where i can't seem to figure out how to save a Gameobject as it has been positioned at a certain spot which is used to select a spawnpoint where the selected object shall start.
Basically what i'm trying to do is to select 5 characters and put them in certain positions by clicking on them. Whatever position you choose for a character the character should spawn in a corresponding place in the next scene. but i don't know how to transfer them over to the right place.
i should probably mention that i know the basics of Unity and is a complete beginner.
not quite clear on what you are trying to "Save". Is it just the positions of the spawn points you want to remember? Also, not quite clear on what you mean by "save", usually this means writing a file to disk- are you trying to do this at run-time, or in the editor?
i'm sorry for being vague but what i mean is to save the position in a variable runtime.(or somehting like it). atleast that's how i thought of it. as i said i'm really new so i don't really know the ter$$anonymous$$ology of alot, which really doesn't help me but i'm trying.
Answer by Jessespike · Feb 26, 2016 at 06:06 PM
Variables that are declared with the static keyword will not lose the data on scene loads. So you can store the character positions in a static variable in one scene, and load from it in the next scene.
Attach the script to an empty GameObject in both scenes.
using UnityEngine;
public class CharacterSpawnPositions : MonoBehaviour {
public static Vector3[] positionArray = new Vector3[5];
}
Then you can access the positions directly from other scripts:
// store the character's position
CharacterSpawnPositions.positionArray[0] = transform.position;
// position the character
transform.position = CharacterSpawnPositions.positionArray[0];
great code. the only problem being that the code above doesn't give, let's say position 0 in the array, a specific value for just that character so that i could use that value in the next scene. which is where the spawnpoint is to check if the character has a matching value that has been given to it.
Script 1 character has entered a specific place with a trigger, assign a value. should be doable for more than 1 at the same time
script 2 the spawnpoints check for it's assigned value and tries to move another object with the same value to it
P.S: This may be horrible way of doing this and i'm just talking nonsense. in that case, please correct me. Thanks for all the Help :)
Sure it does. However, where he uses "5" you need to use the "number of players". Furthermore, each player will need to know/store it's "player number". With this in place you can simply use the player's number to index into the array. e.g.
// store the character's position
CharacterSpawnPositions.positionArray[player.playerNumber] = player.transform.position;
// position the character
player.transform.position = CharacterSpawnPositions.positionArray[player.playerNumber];
[note: since you will using a variable for the size of the array, you will need to initialize it in start(), rather than inline with the declaration like in the answer above]
thank you guys for all the answers and helping me. i have now incorporated your code so that a specific character comes to a specific place.
Again, thank you for the help.