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 navybofus · Mar 18, 2014 at 05:06 AM · c#gameobjectbasic programming

Passing a variable to Start() - C#

I have a scene that is setup to battle an opponent. The first time it's called I use a default enemy, due to story line, but when I call the same scene later, I'd like to get a new enemy. So, I overloaded the Start() with Start(int e). Which would allow me to send an enemy id to start the scene.

The problem is, I don't know how to pass a variable to the Start() method. So, I'm guessing that I need to create a script that adds the id to a variable in a GameObject before the scene is called, and then use the Start() method to get and set?

I'm pretty new to programming, so that's probably why I can't figure this out. Basically, I know that there is always a better way to do things that I do. :)

Comment
Add comment · Show 8
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 vexe · Mar 18, 2014 at 09:18 AM 1
Share

A very simple and 'clean' way would be to make an Init method that takes whatever parameters you need, and then just call it from Start - and that other place.

 public void Init(int x, etc)
 {
    // initialize stuff...
 }
 
 void Start()
 {
     Init(...);
 }
 
 // somewhere else...
 meInstance.Init(...);

No need for hacks...

avatar image flamy · Mar 18, 2014 at 09:37 AM 0
Share

shouldn't the object be non destroy-able one for such patter to work?

since he has to reload the whole scene again, is singleton like this helpful?! Sorry if i am missing something?!

avatar image vexe · Mar 18, 2014 at 09:40 AM 0
Share

? - who mentioned singletons? :) - maybe you got confused with meInstance? - I just meant an instance of whatever class he has... no singletons involved.

avatar image vexe · Mar 18, 2014 at 11:17 AM 0
Share

$$anonymous$$aybe if he could give us more information, like who wants to call this Start, we could give him better ideas... Cause he might have tried doing what I said (he mentioned overloading Start...)

avatar image vexe · Mar 18, 2014 at 12:01 PM 1
Share

yea.. also, if he wants to set the information from another scene, he could maybe use a ScriptableObject to store the settings? and so then on his Start method, he could read the enemy ID that corresponds to the current level from that ScriptableObject (of course it has to be saved as an asset) - and then he could change the settings from any level. I think this is a more easy to maintain way...

Another way would be to have a LevelSettings$$anonymous$$anager that has DontDestroyOnLoad on it - and have his levels read the ids from it...

Show more comments

2 Replies

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

Answer by flamy · Mar 18, 2014 at 05:36 AM

In case the start Has to be called from Another scene

Sending parameter to start is not a good solution and it would take a lot to make it work ... instead have a PlayerPref to exchange data from a scene to another

in Start() check this

 int e = 0;
 void Start()
 {
     e = PlayerPref.GetInt("EnemyValue",0);   ///assuming first time the player pref is not set.
 }
 
 void OnApplicationQuit()
 {
     PlayerPref.DeleteKey("EnemyValue");
 }


In the other script set the value of the playerpref to the value you want. Do it at the place where you intend to call the Start with param!. and load level

 PlayerPref.SetInt("EnemyValue",5);
 Application.LoadLevel(0);   /// just an example


In Case start needs to be called from the same scene (the below approach is done by @vexe)

A very simple and 'clean' way would be to make an Init method that takes whatever parameters you need, and then just call it from Start - and that other place.

 public void Init(int x, etc)
 {
    // initialize stuff...
 }
  
 void Start()
 {
     Init(...);
 }
  
 // somewhere else...
 meInstance.Init(...);
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 navybofus · Mar 18, 2014 at 05:40 AM 1
Share

I've been using PlayerPrefs this whole time, and didn't think of that, lol. Thanks flamy

avatar image
1

Answer by haim96 · Mar 18, 2014 at 11:41 AM

i know this is closed question but i would like to point you to a better way to keep data between scenes without using playerpref. the part on DontDestroyOnLoad is relevent for you:

http://unity3d.com/learn/tutorials/modules/beginner/live-training-archive/persistence-data-saving-loading

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

23 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

Related Questions

Multiple Cars not working 1 Answer

How to reference a public gameobjects in script which are attached in the inspector. (solved) 2 Answers

Distribute terrain in zones 3 Answers

C# Check If Gameobject is within Collider 1 Answer

How to store player model and then swap the model to an object I Raycast hit? 0 Answers


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