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 Stealthygolem · Nov 06, 2014 at 01:17 PM · c#variableaccessmethodpublic

Accessing variable from a method in another script and gameObject

Hello. I know this question is kinda basic, but searching through the internet I can't really seem to find an answer. What I want, is to grab a variable from a public method ("public void FireBullet()") in another script from another gameObject. Without the variable being set outside of the method. Because I know that i can access these variables like this: Derp d = (Derp)GetComponent("Derp");

So a visual example of what I want kinda looks like this:

 Derp d = (Derp)GetComponent("Derp")

 //this is what my brain is thinking (syntactical facepalm, though):
 hpLost = d.FireBullet().damageDealt

Any enlightenment here would be greatly appreciated. I've stumbled across this before, and I just had to resort to finding a way to use it as a public variable outside of the method, but it seems like overwork, the list of public variables outside of the methods will get very big.

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
0

Answer by Baste · Nov 06, 2014 at 01:20 PM

This:

 d.FireBullet().damageDealt

Makes no sense. That's not how syntax works. If the FireBullet method returns a float value, then you do

 hpLost = d.FireBullet();

If d saves damageDealt as a public variable after the bullet is fired, you do this:

 d.FireBullet();
 hpLost = d.damageDealt;
Comment
Add comment · Show 3 · 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 Stealthygolem · Nov 06, 2014 at 01:28 PM 0
Share

I still can't understand how to access "damageDealt" from the FireBullet() method in the other script.

using your

     d.FireBullet();
     hpLost = d.damageDealt;

it can't find damageDealt. Sorry for being so confused, and nooblike.

avatar image Linus · Nov 06, 2014 at 01:43 PM 0
Share

The vartiable has to be defined outside of the method. Variables defined inside the method are forgotten between each call to the method.

avatar image Stealthygolem · Nov 06, 2014 at 01:45 PM 0
Share

@Linus, so you are saying I shouldn't try to call variables defined in methods from another script?

avatar image
0

Answer by Mayank Ghanshala · Nov 06, 2014 at 02:01 PM

First You can call Variables on class object only or a static reference of class . Second if you are making a variable inside a method means it is local to that and its scope will be only in that Function.You should consider these are some basic points of progarmming. What you can do you can make that A data type something like this

public float damageDealt{get;private set;}

then call it as usual

 `hpLost = d.damageDealt`

Comment
Add comment · Show 3 · 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 Stealthygolem · Nov 06, 2014 at 02:34 PM 0
Share

What if I want the variable damageDealt to be specific? Can I do this outside of a function?

I know this is also coded wrong, but you get what I want to do here:

     public float damageDealt() {
         attackDamage += Random.Range(2,6);
     }
avatar image Mayank Ghanshala · Nov 06, 2014 at 03:43 PM 0
Share

its a function now

avatar image Stealthygolem · Nov 06, 2014 at 04:22 PM 0
Share

Well, yes.. But, how would I do it any other way? I'm just spit-balling my bad ideas, to try and assist you in helping me, haha.

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

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

Related Questions

Variable Not Changing In Method 1 Answer

How to access gameObject variable script 2 Answers

Accessing Variable from another Script.. with a twist 2 Answers

Why the variable don't change your value? 1 Answer

[C#] Assets/BreakObject.cs(20,62): error CS0165: Use of unassigned local variable `broken' 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