Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
Question by mamahavoc · Feb 26, 2016 at 04:23 PM · c#2dgameobjectselectionspawnpoints

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.

Comment
Add comment · Show 2
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Glurth · Feb 26, 2016 at 05:24 PM 0
Share

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?

avatar image mamahavoc Glurth · Feb 26, 2016 at 05:46 PM 0
Share

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.

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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];
Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image mamahavoc · Feb 26, 2016 at 08:23 PM 0
Share

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 :)

avatar image Glurth mamahavoc · Feb 27, 2016 at 06:29 PM 0
Share

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]

avatar image mamahavoc · Feb 28, 2016 at 08:55 AM 0
Share

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.

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

140 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How to make sprite follow gameObject on trigger? 0 Answers

GameObject overlapping on random spawn 0 Answers

KillPlayer, Null Reference Exception. 2 Answers

Scale of object does not update properly with public variable 1 Answer

How can I make the game object follow the mouse? 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges