Search Unity

How best to reference a Game Object to perform a if mathematical condition is met?

Discussion in 'Community Learning & Teaching' started by Ufreewoody, Jan 15, 2020.

  1. Ufreewoody

    Ufreewoody

    Joined:
    Nov 3, 2017
    Posts:
    16
    Good morning everyone I am working on the "Create With Code" tutorial. I am in Challenge 3 of the
    Prototype 3 This lesson is Balloons, Bombs, & Booleans Bonus 7: The Balloon can float too high

    The bonus asks that
    • Prevent the player from floating their balloon too high
    Hint: Add a boolean to check if the balloon isLowEnough, then only allow the player to add upwards force if that boolean is true

    So my thought was to create a isLowEnough variable then use this variable to check to see if its current Height of the player balloon is less than or equal to the height of the top of the Background object. I am confused on how best to reference the Background game object in my if statement on line 40 of my PlayerControllers.cs script so that I can stop the balloon from exceeding this value.
    What is the best way to accomplish this?

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class PlayerControllerX : MonoBehaviour
    6. {
    7.     public bool gameOver;
    8.  
    9.     public float floatForce;
    10.     private float gravityModifier = 2.5f;
    11.     private Rigidbody playerRb;
    12.     public ParticleSystem explosionParticle;
    13.     public ParticleSystem fireworksParticle;
    14.  
    15.     public bool isLowEnough;
    16.     private float ballonHeight;
    17.  
    18.     private AudioSource playerAudio;
    19.     public AudioClip moneySound;
    20.     public AudioClip explodeSound;
    21.  
    22.  
    23.     // Start is called before the first frame update
    24.     void Start()
    25.     {
    26.         ballonHeight = gameObject.GetComponent<>
    27.         playerRb = GetComponent<Rigidbody>();
    28.         Physics.gravity *= gravityModifier;
    29.         playerAudio = GetComponent<AudioSource>();
    30.  
    31.         // Apply a small upward force at the start of the game
    32.         playerRb.AddForce(Vector3.up * 5, ForceMode.Impulse);
    33.  
    34.     }
    35.  
    36.     // Update is called once per frame
    37.     void Update()
    38.     {
    39.         // While space is pressed and player is low enough, float up
    40.         if (Input.GetKey(KeyCode.Space) && !gameOver && isLowEnough <= Transform.Background.position.y;
    41.         {
    42.             isLowEnough = true;
    43.             playerRb.AddForce(Vector3.up * 1 * floatForce, ForceMode.Impulse);
    44.         }
    45.     }
    46.  
    47.     private void OnCollisionEnter(Collision other)
    48.     {
    49.         // if player collides with bomb, explode and set gameOver to true
    50.         if (other.gameObject.CompareTag("Bomb"))
    51.         {
    52.             explosionParticle.Play();
    53.             playerAudio.PlayOneShot(explodeSound, 1.0f);
    54.             gameOver = true;
    55.             Debug.Log("Game Over!");
    56.             Destroy(other.gameObject);
    57.         }
    58.  
    59.         // if player collides with money, fireworks
    60.         else if (other.gameObject.CompareTag("Money"))
    61.         {
    62.             fireworksParticle.Play();
    63.             playerAudio.PlayOneShot(moneySound, 1.0f);
    64.             Destroy(other.gameObject);
    65.  
    66.         }
    67.  
    68.     }
    69.  
    70. }
     
  2. Ufreewoody

    Ufreewoody

    Joined:
    Nov 3, 2017
    Posts:
    16
    So sorry I put this in this Forum my mistake.
     
  3. rahulkumawat20000

    rahulkumawat20000

    Joined:
    May 29, 2020
    Posts:
    2
    i m getting same error in this challenge 3
     
  4. rahulkumawat20000

    rahulkumawat20000

    Joined:
    May 29, 2020
    Posts:
    2
    same bro