Activate disable elements from array

This is a bit confusing to explain but I will do my best.

I have 2 gameobjects Road0 and Road1, each with 4 sprites to render on canvas.
I created an array for each group of elements to control looping of those sprites for each Road.
I can disable all elements of first array to render the second array, but I can not re-activate those elements of the first array again.

This is my code:

public class RoadSequence : MonoBehaviour {

    [SerializeField]
    private RoadSequenceType type;
    public RoadSequenceType Type
    {
        get { return this.type; }
    }

    [SerializeField]
    private RoadPiece[] roadPieces;
 
    [SerializeField]
    private bool loop4x512= false;
 
    private bool activated = false;
    public bool Activated
    {
        get { return this.activated; }
        set { this.activated = value; }

private void LoopPieces(RoadPiece piece1, RoadPiece piece2,RoadPiece piece3,RoadPiece piece4)
    {
       
if(piece1.PieceTransform.localPosition.y < -511 )
        {
            piece1.PieceTransform.localPosition = new Vector3(piece1.PieceTransform.localPosition.x,piece4.PieceTransform.localPosition.y+512,0);
        }

        if(piece2.PieceTransform.localPosition.y < -511 )
        {
            piece2.PieceTransform.localPosition = new Vector3(piece2.PieceTransform.localPosition.x,piece1.PieceTransform.localPosition.y+512,0);
        }

        if(piece3.PieceTransform.localPosition.y < -511 )
        {
            piece3.PieceTransform.localPosition = new Vector3(piece3.PieceTransform.localPosition.x,piece2.PieceTransform.localPosition.y+512,0);

if(piece4.PieceTransform.localPosition.y < -511 )
        }
piece4.PieceTransform.localPosition = new Vector3(piece4.PieceTransform.localPosition.x,piece1.PieceTransform.localPosition.y+512,0);
        }
    }

private void MoveRoadPieces()
    {
        for (int i = 0; i < this.roadPieces.Length; i++)
        {

            this.roadPieces [i].PieceTransform.Translate (
                    this.moveDir * (this.gameScreen.Velocity * this.speed) * Time.deltaTime);
        }

        if(this.loop4x512)
        {
            this.LoopPieces(this.roadPieces[0], this.roadPieces[1], this.roadPieces[2], this.roadPieces[3]);
            this.LoopPieces(this.roadPieces[3], this.roadPieces[2],, this.roadPieces[1], this.roadPieces[0]);
        }

    }

And the code from Gamescreen Script:

public enum RoadSequenceType
{
    Road0 = 0,
    Road1 = 1
}

private void ActivateRoadSequence(RoadSequenceType type)
    {
if(type.Equals(this.currentType))
            return;

        for (int i = 0; i < this.roadSequences.Length; i++) {

            if(this.roadSequences[i].Type.Equals(type))
                this.roadSequences[i].Activate();

            else

                this.roadSequences[i].Activated = false;
        }
    }

    private void LaunchRoadSequences()
    {

        if (scoreNum <= 30) {
            this.ActivateRoadSequence (RoadSequenceType.Road0);

        } else if (scoreNum > 30 && scoreNum <= 70) {

            this.ActivateRoadSequence (RoadSequenceType.Road1);

            }else if (scoreNum > 70 && scoreNum <= 100) {

            this.ActivateRoadSequence (RoadSequenceType.Road0); // Don t work !!

        }
}

Can you see how I can turn on again Road0

We’re missing a piece of the puzzle, such as what calls LaunchRoadSequence and the Activate function does, but my guess would be, you put your script on the elements you are going to deactivate, and then expect their Update, or some such method, to activate them. The thing is, a disabled object will not run it’s script’s Update.

It’s also missing the variable declaration of “currentType”.
It’s also worth pointing out, if you didn’t know, that you can omit “this” unless there is ambiguity in what’s being called.
quick example:

class Blah {
int num;
int newnum(int num){
this.num = num;
// of course, because without 'this' it would mean the scoped variable. but...
int newnum(int i){
num = i;
}
// is perfectly okay.
}

methos5K this is where “currentType” is defined (gamescreen script)

[SerializeField]
    private RoadSequence[] roadSequences;

    private RoadSequenceType currentType = RoadSequenceType.None;
    public RoadSequenceType CurrentType
    {
        get { return this.currentType; }
        set { this.currentType = value; }
    }

I already remove “this” from almost of my code :slight_smile:

To solve my problem I use the method of duplication, that is, I duplicate the arrays and assign another value. I’m afraid this method will increase the size of the game or occupy more memory unnecessarily.

Road0 have 17 MB of images and if I duplicate them in the hierarchy what consequences can this have?

You missed what the poster before me said, I think… about LaunchRoadSequence and Activate().
I’m just waking up, so can’t read over all of this properly just this second.

If you duplicate references to things, you add on only the size of the references, not the data it references**.

After some holidays to refresh the brain, i figure out the solution

roadSequences [0].gameObject.SetActive(false);
            ActivateRoadSequence (RoadSequenceType.Road2);

This way I can activate or desactivate the elements of array.

It s simple I Know :slight_smile: