2
votes

I am a bit of a noob to programming and I am trying to make a GameObject , deactivate and reactivate over a set amount of seconds.For example I want my star to slowly blink before it goes away, to create a cool looking effect. If there is a better way of using this method done with out using SetActive(false) and what not , please feel free to give me your method - This is my code , Sorry if its messy i gotta get better at this but i will in due time

Thanks guys

//Timers
public float ScoreTimer;
public float[] TimeMarkStamp;

//Scoring
public int totalCollStars;
[Space]
public int maxStars = 5;

public GameObject[] StarImages;


// Use this for initialization
void Start()
{


}

// Update is called once per frame
void Update()
{
    ScoreTimer += Time.deltaTime;
    if (ScoreTimer <= TimeMarkStamp[0])
    {
        Debug.Log("It works");
        StarImages[0].SetActive(false);
    }
    else if (ScoreTimer <= TimeMarkStamp[1])
    {
        Debug.Log("It workds" + TimeMarkStamp[1]);
        StarImages[1].SetActive(false);
    }
    else if (ScoreTimer <= TimeMarkStamp[2])
    {
        Debug.Log("It works" + TimeMarkStamp[2]);
        StarImages[2].SetActive(false);
    }
    else if (ScoreTimer <= TimeMarkStamp[3])
    {
        //This is not working 
        InvokeRepeating("flickerEffect", 3f, 1f);
    }
}

void flickerEffect()
{
    bool flickCheck = false;


    if (flickCheck == false)
    {
        StarImages[3].SetActive(true);
        flickCheck = true;
    }
    else if (flickCheck == true)
    {
        StarImages[3].SetActive(false);
        flickCheck = false;

    }
}

}

1
Try looking/googling into the Coroutines, but the programming itself must be pretty strong with you if you are to make games.AgentFire
true, simplest method would be to make use coroutines, but depending on what to achieve you may want to use other methods of making objects invisible.Łukasz Motyczka
Okay awsome thanks heaps i will have a good look into itkeanu.b

1 Answers

2
votes

If there is a better way of using this method done with out using SetActive(false) and what not

Yes, there is a better way, other than using the SetActive function. You should change the alpha color of the GameObject from 0 to 1 back and forth. After that you can then disable the GameObject with SetActive. This saves how much garbage would have been generated when repeatedly calling the SetActive function.

If this is a 3D GameObject, change the Rendering Mode from Opaque(default) to Fade or Transparent.

A simple function that can do this:

void blink(GameObject obj, float blinkSpeed, float duration)
{
    StartCoroutine(_blinkCOR(obj, blinkSpeed, duration));
}

IEnumerator _blinkCOR(GameObject obj, float blinkSpeed, float duration)
{
    obj.SetActive(true);
    Color defualtColor = obj.GetComponent<MeshRenderer>().material.color;

    float counter = 0;
    float innerCounter = 0;

    bool visible = false;

    while (counter < duration)
    {
        counter += Time.deltaTime;
        innerCounter += Time.deltaTime;

        //Toggle and reset if innerCounter > blinkSpeed
        if (innerCounter > blinkSpeed)
        {
            visible = !visible;
            innerCounter = 0f;
        }

        if (visible)
        {
            //Show
            show(obj);
        }
        else
        {
            //Hide
            hide(obj);
        }

        //Wait for a frame
        yield return null;
    }

    //Done Blinking, Restore default color then Disable the GameObject
    obj.GetComponent<MeshRenderer>().material.color = defualtColor;
    obj.SetActive(false);
}

void show(GameObject obj)
{
    Color currentColor = obj.GetComponent<MeshRenderer>().material.color;
    currentColor.a = 1;
    obj.GetComponent<MeshRenderer>().material.color = currentColor;
}

void hide(GameObject obj)
{
    Color currentColor = obj.GetComponent<MeshRenderer>().material.color;
    currentColor.a = 0;
    obj.GetComponent<MeshRenderer>().material.color = currentColor;
}

Usage:

void Start()
{
    blink(gameObject, 0.2f, 5f);
}

If this is a SpriteRender, you have to replace all the obj.GetComponent<MeshRenderer>().material.color code with obj.GetComponent<SpriteRenderer>().color.