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 Rekaens · May 20, 2010 at 01:58 PM · referencerpg

Variables acesssed by other scripts??? and change int values

i am now making an Rpg very basic, and i put this in var lvl : int; var hp : int; var maxhp : int; var mp : int; var maxmp : int; var atk : int; var def : int; var mag : int; var xp : int; var maxxp : int;

function OnGUI () { GUI.Label (Rect (Screen.width / 30, Screen.height / 35 + 1,220,345),"LvL: " + lvl); GUI.Label (Rect (Screen.width / 30, Screen.height / 35 + 11,220,345),"HP: " + hp + "/"+ maxhp); GUI.Label (Rect (Screen.width / 30, Screen.height / 35 + 21,220,345),"MP: " + mp + "/"+ maxmp); GUI.Label (Rect (Screen.width / 30, Screen.height / 35 + 31,220,345),"XP: " + xp + "/"+ maxxp);

}

as you can see i have made variables for max and "current" status for what its needed for what do i have to do to make fx a collider increase xp, in a different script so fx: "first person controller with this script equipped, walks into a collider, and gains 10 xp so it will show xp gained in the window?

i know this is maybe hard but hope you are up to it :D thansk alot in advance!

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

1 Reply

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

Answer by Mike 3 · May 20, 2010 at 03:24 PM

You need to get hold of a reference to the script doing the GUI.

First off, you probably need to get hold of the GameObject, using one of these methods: http://unity3d.com/support/documentation/ScriptReference/index.Accessing_Other_Game_Objects.html

Secondly, you need to get your script component from that gameobject reference: http://unity3d.com/support/documentation/ScriptReference/index.Accessing_Other_Components.html

Once you have that, you can pretty much just do something along the lines of:

yourScriptVariable.xp += 50;

This, however, is considered bad practice, so I would strongly suggest adding a function to increment the XP into your script, accessing it like this:

yourScriptVariable.GiveXP(50);

This has the benefit of making it very easy to pop up XP changes on screen, as well as work out if you levelled (and any actions because of this, like ding sounds or messages)

Example:

function GiveXP(addedXP : int) { if (addedXP < 0) { Debug.Log("Can't remove exp!"); return; }

 xp += addedXP;
 if (xp &gt; maxxp)
 {
     //level up here
 }

}

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 Rekaens · May 20, 2010 at 05:02 PM 0
Share

hmmm, im pretty new to this whole scripting, but im slowly learning, so im still pretty lost :( no idea how to make the things work you posted, how do i make a function to "increment" my xp??

i tried this:

var target : charstats; function Update () { target.xp +=2; }

and that gives me no script errors, but! how do i do it your way? the GiveXP way? can you sahre your knowledge i would be most happy! thansk alot in advance!

avatar image Mike 3 · May 20, 2010 at 05:35 PM 0
Share

added an example for you at the end

avatar image Rekaens · May 20, 2010 at 06:05 PM 0
Share

then i get a bunch of errors (10,5): BCE0005: $$anonymous$$ identifier: 'xp'. (11,9): BCE0005: $$anonymous$$ identifier: 'xp'. (11,14): BCE0005: $$anonymous$$ identifier: 'maxxp'.

thank you for your patience mate

avatar image Mike 3 · May 20, 2010 at 06:45 PM 0
Share

are you putting the function into the same script as all your variables in the original question?

avatar image Rekaens · May 20, 2010 at 07:03 PM 0
Share

Haha! i feel so freaking stupid now hahaha! sorry man! it works thank you so so much! you got a check mark now, love ya!

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

How to reference the current colliding object's script 1 Answer

Can I make reference/assocation to another component as variable? 0 Answers

Change gameobject sprite with button 1 Answer

Copying GameObjects with reference types 1 Answer

Use Of Static Variables 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