• 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 jbickel · Aug 06, 2011 at 10:18 PM · global

Issue with globals.

I am learning Unity using JavaScript. I'm not a noob to scripting in general, but am at the beginning of this particular learning curve. I did several searches, but found nothing relevant (although I did get an error page saying the the search engine was taking a break at some point).

Here is the background...

I have an empty object that casts a ray. This empty object stores where the ray hits as global Vector3 variable.

I am intending to use another script attached to the main camera to use that global Vector3 to place a retical graphic in 2d space via WorldToScreenPoint.

The issue is that it doesn't see the global variable. Here is the relevant code for the raycaster...

 //targetRetical.js

 static var tigerRetical : Vector3 = Vector3.zero;
 
 function Update () {
 
 var hit : RaycastHit;
 
 if (Physics.Raycast (transform.position, transform.TransformDirection (Vector3 (0,0,-1)),hit, 150)) {
     
     var tigerRetical : Vector3 = hit.point;
     }
 }

And here is the script that is attempting to read the global...

 var target : Vector3 = (targetRetical.tigerRetical);
 
 function Update() {
     print ("vector is " + target);
     }

Basic troubleshooting that I have done so far is...

  1. Printing the value of "targetRetical" in the first script. This works as expected.

  2. Printing the value of "target" in the second script. This does not work as expected. It prints the vector as "0.0, 0.0, 0.0".

  3. I have added a particle emitter to visually confirm that the raycast moves with the object casting the ray in script #1. This works as expected.

  4. I have also confirmed that I was creating globals and reading them from other scripts correctly by creating a global Int in script #1 and printing it successfully in script #2.

I just don't get what I'm missing...

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
Best Answer

Answer by aldonaletto · Aug 06, 2011 at 11:11 PM

The problem is in the if below: the keyword var created a new temporary variable tigerRetical and stored the hit.point in it - the original static tigerRetical never saw a clue of the hit.point. Remove the variable declaration like below and everything will work:

if (Physics.Raycast (transform.position, transform.TransformDirection (Vector3 (0,0,-1)),hit, 150)) {
    tigerRetical = hit.point; // store hit.point in the real tigerRetical!
}

EDITED: Ok, I didn't see it: you're assigning tigerRetical to target outside any function, so it will be executed only once when the script is instantiated. You must place this assignment inside Update to have it updated (intended pun):

function Update() {
    var target: Vector3 = targetRetical.tigerRetical; // you don't need the parenthesis
    print ("vector is " + target);
}
If you declare target outside the function, it will have script scope - the variable will be known by everybody in the script, and will retain its contents while the script is alive. Declaring it inside Update, like above, it will have local scope, which means it will exist and be known only inside the function.
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 jbickel · Aug 06, 2011 at 11:52 PM 0
Share

That makes sense, but still doesn't fix it. I made the suggested change and I added a "print (tigerRetical);" in the first script and it worked fine, but it still came back as all 0's in the second. I just don't get it.

avatar image aldonaletto · Aug 07, 2011 at 12:22 AM 0
Share

The problem is the variable being only initialized to targetRetical.tigerRetical. Take a look at my answer - it was edited to explain this problem.

avatar image jbickel · Aug 07, 2011 at 12:35 AM 0
Share

BOO$$anonymous$$!!! That's it! I knew it had to be something simple that made sense. Thanks for the extra set of eyes on this ($$anonymous$$e were a little tired from looking at the same problem)!

avatar image
0

Answer by StephanK · Aug 06, 2011 at 10:22 PM

I'm not sure if this behaves the same in JS than in C#, but in C# Vector3 is a struct, not a class, so you don't get a reference to the Vector3, but a copy of the Vector. This is never changed so it will always be zero.

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 jbickel · Aug 06, 2011 at 10:30 PM 0
Share

Hmmm..

Where do I change that at? I know I have seen something about script execution order somewhere while exploring, but can't remember where.

That and I think it a bit odd that the one time I try to do this with an integer it works, but didn't work over at least a dozen times while trying it with a Vector3. I only have four scripts in this project so far.

avatar image jbickel · Aug 06, 2011 at 10:38 PM 0
Share

Never$$anonymous$$d. I found it via a youtube video. I'll see if it works...

avatar image jbickel · Aug 06, 2011 at 10:51 PM 0
Share

It's still not working. I placed the first script at -100 and the second at -50 in the execution order and I am still getting 0.0's across the board printing in the second script.

I also tried setting the initial value as...

"static var tigerRetical : Vector3 = Vector3.up;"

And it still came back all 0's...

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Getting the (instantaneous) normal of a meshfilter object 2 Answers

Global Variables: import script (boo) 2 Answers

Local-Global error. 1 Answer

Camera rapidly changes it's position when the parent changes. 0 Answers

Local Look Rotation 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