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 qewo · Jul 25, 2013 at 07:14 PM · referenceraypassjavascript arrays

Send javascript array to a ray hit object

here is my keyring weapon

 #pragma strict
 
 var rangeChange : float = 3.0;
 
 var keyNum = new Array();
 
 function Start () {
 
     keyNum.Add(1);
 
 }
 
 function Update () {
 
     if (Input.GetButtonDown ("Fire1")) {
     
         var range = rangeChange;
         
         var layerMask = 1 << 8;
     
         var hit : RaycastHit;
         
         var door : String;
     
         if (Physics.Raycast (transform.position, transform.TransformDirection (Vector3.forward), hit, range, layerMask)) {
             print ("Did Hit");
             door = hit.collider.gameObject.GetComponent("Door_Slide");
             door.KeyLock(keyNum);
         } else {
             print ("Did not Hit");
         }
     }
 
 }

and here is my receiving door

 #pragma strict
 
 var locked : boolean;
 var keyLockNum : int;
 
 function KeyUnlock(keyNum : int[]) {
 
     if (locked == true && (keyLockNum in keyNum)) {
     
         locked = false;
     
     }
 
 }

I'm trying to send a list of obtained keys represented by int values in an array, i would like to be able to send them to any object on layer 8 (my usable layer) and then check if the key required for the door (set in the inspector for ease of replicating) is held by the player. I have been trawling all over the forums looking for an answer and I'm stuck, any help would be much appreciated :D

Comment
Add comment · Show 1
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 Eric5h5 · Jul 25, 2013 at 09:31 PM 0
Share

Really should not be using JS Array. Use a built-in array or generic List.

1 Reply

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

Answer by Linus · Jul 25, 2013 at 07:38 PM

Keyringweapon

 #pragma strict
  
 var rangeChange : float = 3.0;
  
 var keyNum = new Array();
  
 function Start () {
  
 keyNum.Add(1);
  
 }
  
 function Update () {
  
 if (Input.GetButtonDown ("Fire1")) {
  
 var range = rangeChange;
  
 var layerMask = 1 << 8;
  
 var hit : RaycastHit;
  
 var door : Door_Slide; //Name of the script, is the type
  
 if (Physics.Raycast (transform.position, transform.TransformDirection (Vector3.forward), hit, range, layerMask)) {
 print ("Did Hit");
 door = hit.collider.gameObject.GetComponent(Door_Slide); //No need for quotes, as we are giving the type
 //Make sure to call the right function
 door.KeyUnlock(keyNum);
 } else {
 print ("Did not Hit");
 }
 }
  
 }

receiving door

 #pragma strict
  
 var locked : boolean;
 var keyLockNum : int;
 
 
 function KeyUnlock(keyNum : Array) {
 //Check to see that the array is being sent in
 for(k in keyNum){
  Debug.Log('key: '+k);    
 }
 
 
 if (locked == true && (keyLockNum in keyNum)) {
  
 locked = false;
  
 }
  
 }



You defined the wrong array type in the function defenition.

Also see http://wiki.unity3d.com/index.php?title=Which_Kind_Of_Array_Or_Collection_Should_I_Use? and perhaps consider using Generic List instead of the JS array.

Edit: I first wrote ArrayList I meant Generic List. Edit 2: Did not notice the wront type for the door variable

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 qewo · Jul 25, 2013 at 08:25 PM 0
Share

I think my array now exists in my door script and my keyring script but i can't get the keyring to send the array to the door that was hit by the ray.

door script

 var locked : boolean;
 var keyLockNum : int;
 var keyNum = new Array();
 
 function $$anonymous$$eyUnlock(keyNum : Array ) {
 
     if (locked == true && (keyLockNum in keyNum)) {
     
         locked = false;
     
     }
 
 }

my keyring script

 #pragma strict
 
 import System.Collections.Generic;
 
 var rangeChange : float = 3.0;
 
  var keyNum = new Array();
 
 function Start () {
 
     keyNum.Add(1);
 
 }
 
 function Update () {
 
     if (Input.GetButtonDown ("Fire1")) {
     
         var keyNum : List.<String>;
         keyNum = new List.<String>();
     
         var range = rangeChange;
         
         var layer$$anonymous$$ask = 1 << 8;
     
         var hit : RaycastHit;
         
         var door : String;
     
         if (Physics.Raycast (transform.position, transform.TransformDirection (Vector3.forward), hit, range, layer$$anonymous$$ask)) {
             print ("Did Hit");
             door = hit.collider.gameObject.GetComponent("Door_Slide");
             door.$$anonymous$$eyLock(keyNum);
         } else {
             print ("Did not Hit");
         }
     }
 
 }

i tried using a generic list but the import System.Collections.Generic; seems to bug out.

thanks for the help so far but i still need a bit more help :)

avatar image Linus · Jul 25, 2013 at 09:23 PM 0
Share

Are you aware that you are calling $$anonymous$$eyLock but the function name is $$anonymous$$ey*Un*lock()

avatar image qewo · Jul 25, 2013 at 09:32 PM 0
Share

oops :\ although fixing that still stops it from working i'm getting the error

Cannot convert 'UnityEngine.Component' to 'String'.

and

'$$anonymous$$eyUnlock' is not a member of 'String'.

but i can't see another way to direct to the object that was hit with the ray, sorry thanks for the help though.

avatar image Linus · Jul 25, 2013 at 10:00 PM 0
Share

If you go back to your original sample code, and apply what I wrote function $$anonymous$$eyUnlock(keyNum : Array ) { It should work. The variable keyNum's type should match in the function defenition as it is in the variable declaration. Hope I made sense there.

Your second example is very flawed. Ill try to improve my answer to match your question better.

avatar image qewo · Jul 26, 2013 at 09:26 AM 0
Share

Thank you so much! I see where I was messing up by attempting to use a string ins$$anonymous$$d of the script name :D Apparently I need 15 rep to be able to thumb you up but when I get 15 rep ill return to this post and give it you :D

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

17 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

Related Questions

How to pass UnityEvent object as method Parameter 0 Answers

Getting a reference to a class created in a different gameObject (Javascript) 1 Answer

GUI problem: Lack of Events and Listeners. 1 Answer

Default References of a Script 1 Answer

PDF scripting reference? 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