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 Aaron Ratcliffe · May 03, 2011 at 01:18 PM · movementscenevariablespeed

Changing character run speed based on another scripts variable

Basically i have a game that consists of 2 mini levels per level, In the first level the player drops a ball and depending on how many obstacles they hit they are awarded a score, for example "player hit 2 obstacles giving them a score of 2"

The second section on the level involves the player controlling a character from a top down view avoiding other obstacles. I would like to use the score from the previous scene to modify the speed at which the player moves (i know how to manipulate the speed but not how to access the variable from the script in the last scene)

Answers in javascript would be much appreciated.

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

2 Replies

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

Answer by Rabwin · May 03, 2011 at 03:04 PM

First preference is to use "Don't Destroy on load" which keeps certain objects when a new scene loads, which means you can carry over your "Main" controller object holding the score and access it as a static variable.

DontDestroyOnLoad (gameObjectWhoIsHoldingScore);

The other more hacky way is to store it using the playerprefs in-built unity function.

PlayerPrefs.SetInt("Score", scoreVar);

then in the new scene:

SpeedModifier = PlayerPrefs.GetInt("Score");
PlayerPrefs.DeleteKey("Score");

Take your pick. I advise having a look at both in the scripting reference.

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
avatar image
0

Answer by red 2 · May 03, 2011 at 02:59 PM

create a function that returns the variables value you need, like:

var xyz = 1;   
function returnXyz(){
    return xyz;
}

then it can be accessed by another script via

var _script : ScriptName;
var _script = GetComponent(ScriptName);
// some code here
_script.returnXyz();
//...

(in this case both scripts would be components of the same GameObject, modify as you need)

edit: this only works if your 2 mini levels are in one scene, which i kinda thought of now =/ otherwise nvm

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 Rabwin · May 03, 2011 at 03:05 PM 0
Share

I think he wants to pass variables thorugh to the new scene. So won't a get accessor not work? (I'm still new to code so forgive me)

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

Adjust turning speed for 3rd Person Character? 1 Answer

Movement in my terrain ? 2 Answers

2D Character Movement 1 Answer

Pushing rigidbodies around 1 Answer

I'm trying to make my player move a certain speed I want it to go, but I'm having a hard time. (3D project) 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