How, from object A, to get an array stored in object B with a raycast?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Dostoi

with the following code, I can access an object with a colliding raycast, get a single string and add it to a textbox.

Object A

        var test = _raycast.GetCollider().Get("testString");
        _richTextLabel.Text = test.ToString();

Object B

public string testString;
public String[] stringArray = new String[5];

	testString = "anything"

Unfortunatly, replacing “testString” by “stringArray” only seem to return a string called “System.String” (which I’m not able to parse to a textbox).

What I’m trying to do is to replace “testString” by an array (“stringArray” in the above example) that would be set in the inspector of object B.

How can I get that “stringArray” from object B with a raycast and store the strings contained to a variable in object A?

Thank you for your time.

it looks like you defined stringArray as array of 5 strings. Did you tried to output it’s value as test[0].ToString() in this case?

AlexTheRegent | 2022-03-01 06:30

Yep. But, since the array from object A is empty (and not filled as I wanted by the strings from the array of object B) it crashes the game as it tries to add a “null” value “To string” (ToString()). But I can see your point, and that’s what’s bugging me. The following code from object A returns indeed “System.String” from object B :

var test = _raycast.GetCollider().Get("stringArray");

But… I am completely puzzled as of how I could extract a string from that “System.String”.

Dostoi | 2022-03-01 10:02

:bust_in_silhouette: Reply From: Zylann

Maybe try that:

B b = _raycast.GetCollider() as B;
if(b != null)
	// However you can't just do that.
	// The reason is simple: `Text` is a string, `stringArray` is an array of strings.
	//_richTextLabel.Text = b.stringArray;

I’m just not sure what you want to do with your array of strings once you get it from the raycast. Do you want to concatenate the strings? Or show multiple lines where each line is one element of the array?

To do the latter, you can do that:

_richTextLabel.Text = string.Join("\n", b.stringArray)

Thank you for your answer and time. Actually, in my game, there’s a room filled with objects. Object A is the player, and objects B represents any object that can be examinated. For each object “B”, there is a script attached that contains an array of strings. For example “{description_A_1; description_A_2;}”. Back to object A, I have a dictionary (a converted .json file) that is loaded and which contains all the text lines of the game. So, what I am trying to do is to catch that array from object B through a raycast. Then I would “somehow” (and this is where I’m stuck ^^) copy it to object A. Finally, those strings could be used as a reference of which lines from the dictionary should be displayed in the textbox.

About your proposition. Isn’t “B b = _raycast.GetCollider() as B;” the same as “var B = _raycast.GetCollider();”? What it returns is the reference of the object that the raycast collide with (the rigidbody in my case). Am I misunderstanding something? (Thank you again for your time)

Dostoi | 2022-03-01 15:55

Isn’t “B b = _raycast.GetCollider() as B;” the same as “var B = _raycast.GetCollider();”?

No. var B = _raycast.GetCollider(); will not compile because GetCollider does not return type B. You have to cast it to obtain B. So you could write (B)_raycast.GetCollider(), but the result can also be null if there is no collision, and if there is, there is no guarantee it will be a B. So that’s why I chose as B instead.

What it returns is the reference of the object that the raycast collide with (the rigidbody in my case)

I did this choice instead of Get("stringArray") also because it is typed and faster, contrary to the latter.

Zylann | 2022-03-01 21:52

Very informative. Thank you.

Dostoi | 2022-03-02 08:16

:bust_in_silhouette: Reply From: Dostoi

The answer is: I-nearly-got-it-right-from-the-beginning. So, for anyone interested, the following code…

var test = _raycast.GetCollider().Get("stringArray");

… returned, with “GD.print()”, as stated above, “System.String” (which I thought was just some kind of generic answer of the program). But it really was the array. Only, it was stored as an object. So what simply had to be done was that:

var test = _raycast.GetCollider().Get("stringArray") as String[];

And then GD.print(test[0]) will return the first string of the array.