Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 /
avatar image
1
Question by SpiritWebb · Nov 27, 2009 at 03:08 PM · scenelinking

Scene Connecting

Alright,

So I ran into a bit of a problem with the linking.

I know that when creating a new level (new star system in my case) you place the player down where they will arrive/start.

Well going to the new star system works beautifully. My question is, when I return back through the gate, I start where the player first starts on the otherside of the system.

How can I correct this to get the player to actually arrive at each gate?

Comment
Add comment
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

5 Replies

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

Answer by Bampf · Nov 27, 2009 at 03:49 PM

You need at least one object to maintain game state across levels.

Often people have a GameObject called "GameController" or "GameManager" or something like that, that performs game logic and keeps track of information that needs to be passed from level to level. To keep the object from going away, call DontDestroyOnLoad on it.

The tricky part is making sure that you only have one of these objects around. You don't want the first scene to create a new one when the player returns to it. There are various ways around this. See the Unity Wiki's article on Singletons for some good examples. (That code also solves another problem for you, which is how your scripts can easily and efficiently finding the manager object to use.)

Comment
Add comment · Show 2 · 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 SpiritWebb · Nov 27, 2009 at 04:41 PM 0
Share

Confused on the singletons, cause the coding looks like its being done in C#, and I'm doing this in Javascript.

avatar image Bampf · Nov 28, 2009 at 01:19 AM 1
Share

The wiki also has a separate article: A$$anonymous$$anagerClass, which includes a Javascript singleton. It's older and there's less explanation for it but maybe it will help.

http://www.unifycommunity.com/wiki/index.php?title=A$$anonymous$$anagerClass

avatar image
1

Answer by runevision · Nov 27, 2009 at 03:47 PM

In each level you can place some spawn points on the locations where the player should be able to appear. You could have multiple spawn points with different names. You can make a spawn point script that you can attach to an empty game object to make it a spawn point.

You need to have a persistent object (one that is not destroyed when you load a new scene). When entering a gate, you tell this object at which named spawn point in the new scene the player should be spawned at. The persistent object could either be some controller object, or it could just be the player himself.

You can create persistent objects either by using Object.DontDestroyOnLoad. Alternatively, if you only need some persistent data and not a whole gameobject, you can store the data in static variables of a class.

When the new level has loaded, the persistent object spawns the player at the right spawn point, of if the player himself is persistent, you can just move him to the right point. You can use the OnLevelWasLoaded function on the persistent object for that.

Comment
Add comment · Show 2 · 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 SpiritWebb · Nov 27, 2009 at 04:39 PM 0
Share

I don't know how to make a spawn point script, can you point me in the right direction?

avatar image runevision ♦♦ · Nov 30, 2009 at 02:09 PM 0
Share

You probably don't even need a spawn point script. You can just give your spawn points a special tag, and then use FindGameObjectsWithTag to get a list of all spawn points in the current level. Then select the right one of those based on the GameObject names, and move your player to the position of that spawn point GameObject.

avatar image
1

Answer by TowerOfBricks · Nov 27, 2009 at 03:48 PM

The best thing is probably to not create a new player for each scene you load, so tag it with DontDestroyOnLoad if you haven't done so already.

Next, in the player script you would want to put a variable stating if it should start at the start or at a gate, if if it should start at the gate, then define which gate (if you have more than one gate). So when a new scene loads, it will look for if it should place the player at the gate or at the start and place it where it should be.

Comment
Add comment · Show 2 · 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 SpiritWebb · Nov 27, 2009 at 04:40 PM 0
Share

That just confused me...

avatar image Bampf · Nov 28, 2009 at 01:25 AM 0
Share

This is a valid approach, it just keeps the player object around for all levels ins$$anonymous$$d of a separate manager object. In fact you might want to do both, have a manager object and keep the player object from scene to scene.

You still have to figure out when the player object is created and how to keep it from being created more than once. A singleton class is one way. Another is to have each player object destroy itself if it isn't the first one on the scene. I've gotten that way to work but it's a bit fragile and I don't really recommend it.

avatar image
1

Answer by HeywoodFloyd · Jan 08, 2010 at 04:15 PM

I needed to communicate between levels, so I wrote a C# script with nothing but static members and methods. I had a private Dictionary to hold named data, accessor methods to add, remove, or change the stored data.

Because it didn't have anything but static methods, I didn't inherit from Monobehaviour. I had to put it in the standard assets/scripts folder for it to be accessible to other scripts.

This has the advantage of not requiring any special game objects or set up in the Unity editor.

Comment
Add comment · Show 2 · 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 _MGB_ · Jan 15, 2011 at 02:06 PM 0
Share

This sounds like a manual PlayerPrefs class ;)

avatar image HeywoodFloyd · Feb 08, 2011 at 10:13 PM 0
Share

Pretty much. It was a bit more general, as it stored object references, but we used it like the PlayerPrefs class, which I didn't know about.

avatar image
0

Answer by WILEz1975 · Dec 20, 2015 at 05:33 PM

But... if in my scene the player destroy an enemy (gameobject)... when exit ed re-enter (load scene) in this scene... i dont want the destroyed gameobjects...

Comment
Add comment · 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

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

1 Person is following this question.

avatar image

Related Questions

Connecting scenes 3 Answers

Linking a prefab to another prefab in deep hierarchy using Inspector 1 Answer

Enter next level 1 Answer

Unable to merge two projects 1 Answer

how to make my camera follow the character to the scene where my character transported? 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