Search Unity

Bug Help! I've been trying to figure out where this bug comes from and I just can't find it!!!

Discussion in 'Scripting' started by ImprobableDoge, Sep 28, 2021.

  1. ImprobableDoge

    ImprobableDoge

    Joined:
    Aug 4, 2021
    Posts:
    40
    the bug should be around the Update() method, if you need info about what I'm trying to do is I want this object to launch up to the skies, if it reaches y50 it gets launched back down and if it is touching the ground it remains there until a force is applied forcing it to leave the ground, or else it just gets launched back up.

    Also, the debug.logs won't display anything, meaning that something's wrong with the if statement and I don't know what...

    Thanks in advance! ;)

    Here's the code:

    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. public class Blue : MonoBehaviour
    4. {
    5.     private Quaternion halfExtents;
    6.     private Vector3 center = new Vector3(0, -5, 0);
    7.     private float power = -3000.0f;
    8.     private float radius = 5f;
    9.  
    10.     private Vector3 minusForce = new Vector3(0, -1000, 0);
    11.     private Vector3 Force = new Vector3(0, 10, 0);
    12.  
    13.     public GameObject explosionEffect;
    14.     private Rigidbody RigidBody;
    15.     private ConstantForce constantforce;
    16.  
    17.     [SerializeField] private LayerMask GroundMask;
    18.     [SerializeField] private Transform GroundCheckerTransform;
    19.  
    20.     private void Start()
    21.     {
    22.         RigidBody = GetComponent<Rigidbody>();
    23.         constantforce = GetComponent<ConstantForce>();
    24.     }
    25.  
    26.     private void Update()
    27.     {
    28.         //if "blue" is touching on the ground...
    29.         if (Physics.CheckSphere(GroundCheckerTransform.position, 1f, GroundMask))
    30.         {
    31.             //we set the constant force to 0..
    32.             constantforce.force = Vector3.zero;
    33.             //and set gravity to true.
    34.             RigidBody.useGravity = true;
    35.             Debug.Log("it's touching the ground");
    36.         }
    37.         //or...
    38.         else
    39.         {
    40.             //we set gravity to false..
    41.             RigidBody.useGravity = false;
    42.             //we add the default constant force back..
    43.             constantforce.force = Force;
    44.             //and if "blue" is above y 50...
    45.             if (transform.position.y >= 50f)
    46.             {
    47.                 //we try throwing it back down..
    48.                 constantforce.force = minusForce;
    49.                 //"blue" explodes..
    50.                 GameObject explosionInstance = Instantiate(explosionEffect, transform.position, transform.rotation);
    51.  
    52.                 //and affects other rigidbodies in range..
    53.                 Collider[] colliders = Physics.OverlapBox(transform.position, center, halfExtents);
    54.                 foreach (Collider nearbyObject in colliders)
    55.                 {
    56.                     var nearbyBody = nearbyObject.GetComponent<Rigidbody>();
    57.                     if (RigidBody != null)
    58.                     {
    59.                         RigidBody.AddExplosionForce(power, transform.position, radius); //this adds an explosion force.
    60.                     }
    61.                 }
    62.                 Destroy(explosionInstance, 1); //destroys the particles game object.
    63.             }
    64.             else
    65.             {
    66.                 constantforce.force = Force;
    67.                 Debug.Log("HELOO??");
    68.             }
    69.         }
    70.     }
    71. }
     
  2. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,775
    You described the desired behavior, but what happens instead? What's the observed behavior that is different from your desired behavior?
     
    ImprobableDoge likes this.
  3. ImprobableDoge

    ImprobableDoge

    Joined:
    Aug 4, 2021
    Posts:
    40
    What happens is up in my thread where I've described what I wanted to do with it, I don't see anything wrong with it so I don't know why it doesn't work. If you asked for something else could you describe it to me? I'm only a month and a half into programming and I am learning everyday, searched it up on google but I don't know what you mean by it.
     
  4. ImprobableDoge

    ImprobableDoge

    Joined:
    Aug 4, 2021
    Posts:
    40
    OOOOOOOOOH I FOUND OUT!!! I'm such a silly xD

    What happened was that the CheckSphere was checking if it was touching the Layer "Ground", my cube had that layer, meaning that the check sphere was always getting triggered. Forgive me for being such a wind head lol... Thanks!
     
  5. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,775
    What I mean is, the phrase "it doesn't work" is the most useless phrase when asking for help. What doesn't work? Do you get a compiler error? An exception when you run it? Does it do nothing at all? Does it do something, but the wrong thing? Each of these situations involves entirely different troubleshooting steps.
     
    ImprobableDoge likes this.
  6. ImprobableDoge

    ImprobableDoge

    Joined:
    Aug 4, 2021
    Posts:
    40
    It meant that the code didn't work as I wanted it to, no compiling errors or something like that, next time I'm asking for help I'll make sure to remember that... Thanks for the tip and sorry for late reply!