1

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 ();
        }
    }
}
4
  • Where did you define icetextureONfile? What is the type of gameObject? Commented Dec 20, 2015 at 3:23
  • I have never had to define gameObjects before I usually just attach the script right to them and so the connection is made. I was not aware that there were any different types of gameObject, I thought that gameObject was the lowest denominator? Commented Dec 20, 2015 at 3:27
  • Did you attach the object via the editor? If so, try icetextureONfile.gameObject.SetActive(true); Commented Dec 20, 2015 at 3:35
  • No class contains it. I am now trying another method where I have created a new script with a new class, attached that script to icetextureONfile object, and this is the code: public class GameOver : MonoBehaviour { void Start () { gameObject.SetActive (false); } // Update is called once per frame void Update () { if (0 < thingBumps.FixedUpdate.nextKickBacks.Count) { if (thingBumps.FixedUpdate.nextKickBacks.Peek () < kickTimer) { gameObject.SetActive (true); } } } } it doesn't like however that this expression denotes a method group so I got an error Commented Dec 20, 2015 at 3:37

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

7 Comments

I can't imagine why this does not set my object active(false) as it should public GameObject icetextureONfile; void Start() { icetextureONfile.SetActive (false); }
Honestly it should. Did you drag and drop the object into the slot provided by the script?
No I didn't I actually don't know what that means can you please explain where the slot is?
With the gameobject that the script is paired with you should see a spot under the script in the inspector panel if the game object is selected. You can either drag and drop the ice texture from hierarchy or project pane to that slot or click the little circle off to the right of it and it'll pull up a list of all your gameobjects as well.
Oh I understand. Yes that is all set up right. I posted an answer below to show what syntax did work for me. I included active(false) this in the "start" function which works and I'm just now trying to figure out where the active(true) function will work I tried to slip it in immediately after the (-transform up) which is when it should appear chronologically but that does not work for me.
|
0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.