Instantiating Sprite In Script From Prefab and Setting Individual Alpha (Opacity) Levels

I am procedurally generating a 2D tile map by instantiating GameObjects of varying Sprite prefabs, but I am having difficulty in understanding how to properly set the alpha (opacity) value of an individual Sprite without affecting all Sprites instantiated from the same prefab.

In my BoardManager script, I have something like the following:

GameObject segment;
segment = Instantiate(_segmentPrefabs[_segmentControl[seg]], new Vector3(0, seg), Quaternion.identity) as GameObject;
segment.GetComponent<SpriteRenderer>().color = new Color(1f, 1f, 1f, 0.5f);

I would expect this to only affect the GameObject instance I just created and not every GameObject using the same prefab. Additionally, this modifies the prefab’s alpha value in Unity itself, so I have to remember to set it back to the normal value in Unity. This is in no way the behavior I’d expect, coming from other engines (e.g. Phaser/Pixi). Further, it seems more intuitive that I should be able to just modify the alpha channel directly, rather than having to instantiate a new Color object, but this does not seem possible. E.g.

segment.GetComponent<SpriteRenderer>().color.a = 0.5f;

Conversely, I am able to create multiple Sprites in my scene view by dragging the same prefab into the scene and then adjust the alpha values of each individually. This leads me to believe that dragging the prefab objects into the Scene view instantiates the Sprites differently than what I keep reading to do in the script. One other obvious difference I am noticing is that when I create these objects in the script, they are labeled as “copies” when looking at the Hierarchy, whereas the ones I drag into the scene are not labeled as copies.

That said, I’m hoping someone can highlight the differences between adding via the Scene view versus Instantiating in the script, and what I might be doing incorrectly. Thank you!

Edit: To take this a little further, my intention is also to change a Sprite’s texture, when the mouse is over it, and so I imagine I will experience a similar issue when I want to do that.

You could add them to an array and call the prefab in the position of your choice and then set the alpha, also no, you cannot set the alpha directly, the color has to be set to equal a Color object.
Not sure why it doesn’t work, if you were to do something like this you would have a different alpha on each prefab:

for (int i = 0; i < numberofparts; i++)
{
    GameObject newPart;
    newPart = Instantiate(partPrefab, new Vector2(transform.position.x + i, transform.position.y), Quaternion.identity);
    newPart.GetComponent<SpriteRenderer>().color = new Color(0, 0, 0, .25f * i);
}

Isn’t that what I’m doing? I loop through an array of locations and instantiate a GO using the prefab which corresponds to the position of the tile. Is it perhaps that I am doing:

_segmentPrefabs[p] = Resources.Load("seg_p" + p.ToString()) as GameObject;

where ‘p’ is a particular player Id, as opposed to setting the array elements directly to the component in Unity?

Is it possible that you’re setting the segment reference to the prefab somewhere?

This is the bulk of the script. This is a first project, so it’s not at the quality I’d normally put into projects. I’m largely just trying to get an idea of various concepts.

In short, I create a few arrays. In one, I store the various prefab sprites unique to each player at an index which will correspond to the player’s Id. I then set the controlling player of a couple segments (tiles) to be Player 1 and Player 2. Finally, I create all of the Game Objects from the prefabs to create the segments to be displayed.

public class BoardManager : MonoBehaviour {
    private int[] _segmentControl;
    private GameObject[] _segmentPrefabs;
    private Transform _boardHolder;

    void BoardSetup() {
        _boardHolder = new GameObject("Board").transform;
        _segmentControl = new int[GameManager.openSegCount + GameManager.playerCount];
        _segmentPrefabs = new GameObject[GameManager.playerCount + 1];

        // Setup player segment prefab array and their starting points
        for (int p = 0; p <= GameManager.playerCount; p++) {
            // Set segment type prefab array
            _segmentPrefabs[p] = Resources.Load("seg_p" + p.ToString()) as GameObject;

            // Set starting points
            switch (p) {
                case 0:
                    break;
                case 1:
                    _segmentControl[0] = p; // player 1 starts at bottom
                    break;
                case 2:
                    _segmentControl[_segmentControl.Length - 1] = p; // player 2 starts at top
                    break;
                default:
                    // ToDo: Additional player logic
                    break;
            }
        }

        // Create segment sprites
        GameObject segment;
        int totalSegmentCount = _segmentControl.Length;
        for(int seg = 0; seg < totalSegmentCount; seg++) {
            segment = Instantiate(_segmentPrefabs[_segmentControl[seg]], new Vector3(0, seg), Quaternion.identity) as GameObject;
            segment.transform.SetParent(_boardHolder);
        }
    }

You could load your prefabs into a list instead of a game object array so you would do

private List<GameObject> _segmentPrefabs = new List<GameObject>();

then add them to the list

GameObject newSegment =  Resources.Load("seg_p" + p.ToString()) as GameObject;
_segmentPrefabs.Add(newSegment);

then where you have

segment = Instantiate(_segmentPrefabs[_segmentControl[seg]], new Vector3(0, seg), Quaternion.identity) as GameObject;

i’m not sure where you are setting the values of _segmentControl[ ] other than in your switch but you should be able to set the alpha of any game object you instantiate, say you wanted each successive object to be more transparent you could do this

segment.GetComponent<SpriteRenderer>().color = new Color(1f,1f,1f,1-(i*.1f));

if you want to change it later you’ll have to get a reference to it so you might as well add it to a list of gameobjects as well