- Home /
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
Really should not be using JS Array. Use a built-in array or generic List.
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
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 :)
Are you aware that you are calling $$anonymous$$eyLock but the function name is $$anonymous$$ey*Un*lock()
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.
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.
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
![](https://koobas.hobune.stream/wayback/20220613110850im_/https://answers.unity.com/themes/thub/images/avi.jpg)