I have a GameObject that has a sprite array built in the inspector, and I want to use one of the images in a script that’s attached to a different GameObject.
How do I do this in code? I imagine I could reference the GameObject and use a GetComponent, but I’m just not figuring out the right syntax to make it work.
To be more precise the component has the array since a GameObject itself cannot hold such stuff but it can hold components which can hold arrays. So the GO is the container for components.
Just have a public variable of the component with the array in the code of the class which needs it. Then save and drag the object with the array into the slot of the object in need. You don’t even need GetComponent for that since the variable has the correct type already. Note the array also must be public for direct access. But this is probably already the case since you fill it with the inspector.
When new to things, share your entire code for the scripts you need help with. It makes a difference in how easy it is to help you.
MyFirstObj is a gameObject from what you described, so you need to use GetComponent to get the script before you can access the plotSprite array. Or, as suggested, instead of a GameObject variable, you make a variable of type whatever script (MyFirstScript) and drag and drop that gameobject into that slot, which if it has the component on it, it will connect it. Then you could do something like you’re doing.
This is again assuming you already have the “ObjectThatHasTheArray” declared as a GameObject in the new script, and have dragged that object into the correct slot in the inspector. (apologies calling things the wrong name, I’m still learning!).
I am running into a similar issue and I think I am missing something very simple since the Debug.Log works. So here are the codes that matter:
Debug.Log(cardmaster.cardRPG[0].Sprites[0].AttachedSprites[0].name); // This correctly calls the name of the Sprite that is sitting in that position: 1_0.
cardmaster.cardIcon = cardmaster.GetComponent<Sprites>().AttachedSprites[0]; //is giving me issues saying cannot implicitly convert type Sprites to Unity.Sprite. But AttachedSprites is the Property variable for a Sprites Array as seen below:
[SerializeField] Sprite[] sprites;
public Sprite[] AttachedSprites { get { return sprites; }set { sprites = value; } } // there is 1 sprite in this array that I would like to set to the carmaster.cardIcon field that is currently public (I will fix that later).
I am either failing to properly set the value, or I am failing to get the value in the first place. To be fair I am very green, and I am attempting to write Clean Code on my project, all of which points to: Using Properties, writing Modular Code, etc. The above code for Sprites is a Scriptable Object (SO) that functions solely as a Sprite container that has a unique ID for each instance of the SO, and a name which I successfully derived from the name of the SO that was created being transferred to the string field: nameSprite. Point being, I know that it is possible to read Serialized Fields, I know that the proper method of doing so is through a Property (which is what I did to read the name), but when I tried to apply the same principle to assigning the attached Sprite, in the Sprites SO, and the Property Field for a Sprite[0] (the first sprite in the array), I get the error.
If this is phrased incorrectly, or confusing let me know, but this is going to be a huge problem moving forward if I can’t even get the components I created the SO’s for in the first place haha! That being said, should I be creating Monobehaviors for each SO as well? So that not only am I stacking the SO’s within each other, but also through their Monobehavior equivalents? It feels like duplicating code, but then again so do properties and there’s a very good reason that we use properties haha!
Probably not. Please don’t necro-post. If you have a new question, make a new post. It’s FREE!!
When you post, be sure to clarify:
what is the .Sprites[0] array is on line 1 above
what is this Component type you have made called Sprites on line 3 (that’s NOT the same as the Asset type Sprite and of course is also not a SpriteRenderer Component)
what is this field sprites on line 7
ALSO: If you have more than one or two dots (.) in a single statement, you’re just being mean to yourself.
Putting lots of code on one line DOES NOT make it any faster. That’s not how compiled code works.
The longer your lines of code are, the harder they will be for you to understand them.