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
0
Question by desov · Nov 04, 2010 at 04:37 PM · spawnloadlevel

Spawn menu on Level Load

I have created two scenes - menu '1' and level '2'.

Menu has two buttons connected to relaed empty gameobjects (spawnpoints locations).
Level has player inside.

Both buttons and spawnpoints travel across scenes.

  • My problem is: Whitchever button has been pushed, player transfers himself only to one location.

Code inside buttons:

DontDestroyOnLoad (transform.gameObject); var sp : GameObject; function OnMouseUp () { Application.LoadLevel(2); }

function OnLevelWasLoaded (level : int) { if (level == 2) { var gracz = GameObject.FindWithTag("Player"); gracz.transform.position = sp.transform.position; gracz.transform.rotation = sp.transform.rotation; renderer.enabled = false; } }

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

3 Replies

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

Answer by Bampf · Nov 05, 2010 at 09:56 PM

The problem is that regardless of which button is pressed, a new level is loaded. The code that actually moves the player is inside OnLevelWasLoaded. All OnLevelWasLoaded methods will be run for any active objects in the scene. So what's happening is, both buttons are moving the player when the level changes. Whichever script runs last "wins".

Here are two ways to solve this (many others exist.)

If the player object exists in the original scene (and is not destroyed by the level load) then you could move it before loading the new level.

Otherwise, you could record which button was pressed. Add this to the top of the button script:

var fButtonPressed = false;

Then change the OnLevelWasLoaded method:

function OnLevelWasLoaded (level : int) 
{
  if (level == 2 && fButtonPressed)
  {
// etc

My Javascript syntax may be slightly off but that's the idea.

Comment
Add comment · Show 5 · 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 desov · Nov 06, 2010 at 01:51 PM 0
Share

Yeah, that is the idea. Correct me if i am wrong: but1 = false but2 = false if level =2 AND but1 is pressed Do smth if else level = 2 AND but2 is pressed Do smth ???

avatar image desov · Nov 06, 2010 at 01:53 PM 0
Share

I change only the Boolean values in the inspector

avatar image Bampf · Nov 06, 2010 at 02:36 PM 0
Share

In my proposed solution, each button only knows if it was pressed or not. There are two copies of this script (one for each button). One button script will start to run and realize that it wasn't pressed, and do nothing. The other button will know that it WAS pressed, and move the player.

avatar image desov · Nov 06, 2010 at 05:18 PM 0
Share

sorry, I'm missing smth here. I've got 2 buttons with var fButtonPressed = false
The idea wasn't about one button being true and the other false, was it?

avatar image desov · Nov 06, 2010 at 05:52 PM 0
Share

ok ok got it ! :)

avatar image
0

Answer by Bampf · Nov 04, 2010 at 07:00 PM

Doublecheck in the Inspector that the sp variable on each button is set to a different spawn point. It sounds like they are both referencing the same spawn point.

Comment
Add comment · Show 1 · 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 desov · Nov 05, 2010 at 01:46 PM 0
Share

I did. The placement was correct.

avatar image
0

Answer by desov · Nov 06, 2010 at 05:51 PM

Yeah. Got it! Thanks

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

No one has followed this question yet.

Related Questions

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

random spawn locations.... 2 Answers

How to write a simple save script 1 Answer

How to create a spawn point 1 Answer

[solved] respawn when destroyed 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