user5619350
user5619350

Reputation:

Set gameObject active Unity C#

A game object has force applied to it in the positive direction, then after some time it has a force applied to it in the negative direction.

If the force is applied in the negative direction, this means game over and I want to display a totally different gameObject which is the game over gameObject called icetextureONfile. My method is not working I get error "type 'UnityEngine.GameObject' does not contain a definition for icetextureONfile". I am having a hard time refe

public void FixedUpdate() {

// No action happened yet
    gameObject.icetextureONfile.SetActive (false);

// Increase the kick timer 
    kickTimer += Time.fixedDeltaTime;

    // If the next kick time has came
    if (nextKick < kickTimer) {
        // Schedule the kick back corresponding to the current kick
        nextKickBacks.Enqueue (nextKick + 100f);

        // Apply the kick force
        rb.AddForce (transform.up * thrust, ForceMode.Impulse);

        // Plan the next kick
        nextKick = kickTimer + Random.Range (MinKickTime, MaxKickTime);
    }

    // If there are more kick backs to go, and the time of the closest one has came
    if (0 < nextKickBacks.Count) {
        if (nextKickBacks.Peek () < kickTimer) {
            // Apply the kick back force
            rb.AddForce (-transform.up * thrust, ForceMode.Impulse);

            // Game object was kicked down, set active game over object
            gameObject.icetextureONfile.SetActive (true);
            // Dequeue the used kick back time

nextKickBacks.Dequeue ();
        }
    }
}

Upvotes: 1

Views: 8122

Answers (2)

user5619350
user5619350

Reputation:

This syntax worked for me

GameObject.Find ("icetextureONfile").SetActive(false);

As opposed to what is found in the Unity docs

gameObject.SetActive(false);

The first worked for me, the second did not. I am not a great programmer, so maybe it is just a matter of context.

Edit It is commonly known that it is difficult to SetActive(true) after the object has already been SetActive(false). This is because the attached script becomes deactivated too. See Unity forums for details on why this is a nightmare.

To overcome this I have chosen a different route that accomplishes the same thing.

I set the size of the object to 0 until I needed it.

GameObject.Find ("icetextureONfile").transform.localScale = new Vector3(0, 0, 0);

GameObject.Find ("icetextureONfile").transform.localScale = new Vector3(0.02f, 0.02f, 0.02f);

Note that 0.02 is the size of my object in particular and may not be the exact same size as yours.

Upvotes: 0

Jonathan Van Dam
Jonathan Van Dam

Reputation: 648

If your wanting to deactivate one and then activate the other you could just add this to the class make sure its not inside the function

public GameObject iceTexture;

then drag and drop that object into the spot shown in the script in unity called iceTexture. Then just make sure you deactivate the object that the script is attached to and activate the iceTexture object.

gameObject.SetActive(false);
iceTexture.SetActive(true);

This page might help you.

Upvotes: 1

Related Questions