Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Question Trouble Referencing String Array

Discussion in 'Getting Started' started by Rasborealis, Jul 28, 2023.

  1. Rasborealis

    Rasborealis

    Joined:
    Jan 13, 2023
    Posts:
    7
    I have a game object A, and attached to it a script with the same name that creates a string array (B). Script A accesses array B regularly throughout my game and changes entries, and that's working fine. Now I'm trying to get a reference to B from another script(C) attached to instances of prefab D.

    I've been reading and googling and getting increasingly confused with this one. Everything I try out seems to result in errors about the array not being a game object, but also not a component (or I just get a null reference exception). Could someone kindly guide me in the right direction with this? What is the best way to get a reference to this string array?
     
  2. Chubzdoomer

    Chubzdoomer

    Joined:
    Sep 27, 2014
    Posts:
    106
    All you have to do is acquire a reference to object A's script, and then access the array directly if it's public, or indirectly (via a "Getter" for example) if it's private/protected.

    Direct access would look like this:

    objectAScriptReference.StringArray


    Whereas the Getter version would look like this:

    objectAScriptReference.GetStringArray()