C# Syntax issue. grabbing static data from array

After proof reading this, I’m sorry in advance for how confusing this sounds.

I have a targeting script that grabs all objects with a tag of “NPC” and stores them into an array. That works great. On each NPC object, I have a script called, selfObjectIdentifier, this is where I store data on that NPC (name, etc).

From the targeting script, I’m trying to grab the NPC name stored on that object and the logic/syntax I’m using is way off.

C# pseudo code

private string x;

x = target[0].selfObjectIdentifier.myNameIs;


Of course it fails. But hopefully you see what I’m trying to accomplish.

It looks like you should try calling this somewhere in your code.

private string npcName;

void SomeMethod()
    npcName = GetNameOfNPC(0);

string GetNameOfNPC(int index)
    selfObjectIdentifier s = target[index].GetComponent<selfObjectIdentifier>();
    if (s is selfObjectIdentifier)
        return s.myNameIs;
    else if (Debug.isDebugBuild)
        Debug.LogWarning(string.Format("There is no selfObjectIdentifier attached to GameObject {0}!", target[index]), this);
    return null;