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 Sisco1980 · Feb 11, 2015 at 04:45 PM · variablesc++

Access and pass variables between multiple scripts

Hi,

this may be a noobish question, but I have tried searching for this problem for 2 days now and Google isn't my friend. Here's my setup:

I'm trying to make a game, where I use a gameobject that go across multiple scenes, that contains variables (both float, bool's, ints etc.)such as lives, coins and so on. I also have gameobject for each scene that holds variables for specific for only the current scene. All set to be public/static.

My problem is, I can't send or receive data from these scripts and use in scripts in my scenes. Etc. I have a script for money, and when the coin collides with player, it won't send either coins +=1; or coins ++; to the main script with coins;

Also I can't fetch bools or floats from the main script either to check and use to alter gameplay.

Please help me. I'm not a programmer by trade, but I have some insight of coding afterall. I use c++ in Unity 4.6

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 AngryBurritoCoder · Feb 11, 2015 at 04:49 PM 0
Share

c++ in unity ?

avatar image giulio-pierucci · Feb 11, 2015 at 04:59 PM 0
Share

C# maybe!

However, if the gameobjects that you carry from other scenes are single instances, you may write a SINGLETON pattern

http://unitypatterns.com/singletons/

for example, from player script, when collides:

CoinHelper.instance.AddCoins(player)

2 Replies

· Add your reply
  • Sort: 
avatar image
2

Answer by hexagonius · Feb 11, 2015 at 05:01 PM

First, you need to make your GameObject that needs to stay alive across scenes persistent. Check this out: Object.DontDestroyOnLoad

Then, you'll need to grab this from the scripts you have in each scene. Since it is a GameObject not known by them since it is coming from another scene, you should search for it:

     YourScript reference;
     
     void Start(){
     reference = GameObject.Find("TheLivingObject").GetComponent<YourScript>();
     }

From this point on you can call methods as you know it on 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 Sisco1980 · Feb 11, 2015 at 07:57 PM

@hexagonius;

I'd swear I've tested this out before, perhaps some similar lines. But 2 days of struggle rectified in 10 minutes of testing. Received and sended an int variable (coins) with succes. Thanks a lot. :)

I'm tired so I'm going to ask anyway - this goes for all variables right? E.g. setting bools to true/false etc. ?

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 hexagonius · Feb 11, 2015 at 08:34 PM 0
Share

Think of it as the alternative to saving the object from a constructor. Since Unity is usually instantiating GameObjects preset in the scenes for you, you have no other choice than looking for them. But after doing so everything is accessible according to C# accessibility conventions.

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

22 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

Related Questions

Developing for IOS 1 Answer

Perlin Noise. 1 Answer

Steps to create service tool with unity api 1 Answer

Converting c++ to c# and procedural galaxy generation. 1 Answer

unity3d import c++ dll for pass by reference method 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