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 Caiuse · Mar 14, 2011 at 06:35 PM · variablefunction

Using a member variable from one function in another

I've never actually needed to do this until now and I can't understand how.

I'm trying to get the hit.normal variable from one function to use in another function within the same script.

function spawn() { //Im trying to use the hit.normal from GetPointOnMesh function var startRot = Quaternion.LookRotation(hit.normal); var fissurePrefs = Instantiate(fissure, randomPoint.point, startRot); }

function GetPointOnMesh() : RaycastHit{ var hit : RaycastHit; Physics.Raycast (ray, hit, length*2); return hit; }

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

3 Replies

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

Answer by Eric5h5 · Mar 14, 2011 at 06:50 PM

Variables declared inside a function are local to that function only. Since you're returning the RaycastHit from GetPointOnMesh, just call that function instead of using "hit" in your spawn function.

Comment
Add comment · Show 2 · 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 Caiuse · Mar 14, 2011 at 07:32 PM 0
Share

Its returning an error 'normal' is not a member of 'function(): UnityEngine.RaycastHit' I like this solution because it's simple, but its not see$$anonymous$$g to work. any suggestion to get around that error? Cheers

avatar image Eric5h5 · Mar 14, 2011 at 08:11 PM 0
Share

@Caius: var startRot = Quaternion.LookRotation(GetPointOn$$anonymous$$esh().normal);

avatar image
1

Answer by kennypu · Mar 14, 2011 at 06:51 PM

this is more of a general programming question. Its a matter of scoping. Variables created within a function cannot be accessed outside of the function. So to have it be able to be accessed from outside the function, just initialize it outside the function, like so:

var hit:RaycastHit;
function spawn(){...}
function GetPointOnMesh(){...

now the problem here is, you will have to check if hit is null or not before doing your LookRotation. Hope this helps you out.

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 by0log1c · Mar 14, 2011 at 07:14 PM

Kennypu explained the matter of scoping, but you might want to use this approach, I did not test this specific example, but it should work:

function GetPointOnMesh():RaycastHit{ var hit : RaycastHit; Physics.Raycast (ray, hit, length*2); spawn(hit.normal); }

function spawn(thePoint){ var startRot = Quaternion.LookRotation(thePoint); var fissurePrefs = Instantiate(fissure, randomPoint.point, startRot); }

Simply send the hit.normal as a parameter to the next function.

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 Caiuse · Mar 14, 2011 at 07:40 PM 0
Share

(Note: changed the spawn function to spawnFissures) Trying to Invoke method: SpawnFissures.spawnFissures couldn't be called. Its clai$$anonymous$$g that the parameter sent is unreachable. Any ideas? Cheers for the response

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 put a generic value on a function, or in another script? 0 Answers

When issuing to print a private variable from the script, the script prints all the variables attached to objects using the same script instead of the specific object. How do I fix this? 3 Answers

Issues referencing variables between functions 1 Answer

summing up items price in runtime 1 Answer

adding multiple values to single function/variable. 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