I want to make an object fall every 3 seconds, but this does not seem to work. C# Unity

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class FallingObject : MonoBehaviour
{
    // Variables
    float timer = 0;
    MeshRenderer renderer;
    Rigidbody rigidbody;

    // Start is called before the first frame update
    void Start()
    {
        // Cached References
        renderer = GetComponent<MeshRenderer>();
        rigidbody = GetComponent<Rigidbody>();

        rigidbody.useGravity = false;
    }

    // Update is called once per frame
    void Update()
    {
        // Setup to make a timer work every 5 seconds
        timer = timer + Time.deltaTime;
        if((timer >= 6) && (rigidbody.useGravity = false))
        {
            
            // Makes the item fall after 3 seconds
            Debug.Log("3 seconds have passed, now resetting timer.");
            rigidbody.useGravity = true;
            timer = 0;
        }
        if((timer >= 1.25) && (rigidbody.useGravity = true))
        {
            // Waits for the item to hit the ground and then resets it
            Debug.Log("1 seconds have passed, now resetting timer.");
            rigidbody.useGravity = false;
            timer = 0;
            transform.Translate(0,5,0);
        }
    }
}

The comments explain most of what the code is supposed to do, essentially just use Time.deltaTime to create a timer that will alternate between waiting approximately 5 seconds to remove gravity and make the object fall and waiting approximately 1 second to let the object fall to the ground and then resetting said object. For some reason, the code only wants to execute the second if statement, so I’m guessing something is wrong with the rigidbody.useGravity variable. I’m a little new to coding.

>Solution :

Change value assignment to a conditional.

public class FallingObject : MonoBehaviour
{
    // Variables
    float timer = 0;
    MeshRenderer renderer;
    Rigidbody rigidbody;

    // Start is called before the first frame update
    void Start()
    {
        // Cached References
        renderer = GetComponent<MeshRenderer>();
        rigidbody = GetComponent<Rigidbody>();

        rigidbody.useGravity = false;
    }

    // Update is called once per frame
    void Update()
    {
        // Setup to make a timer work every 5 seconds
        timer = timer + Time.deltaTime;
 //Here is where you'd get rid of the value assignment.
        if((timer >= 6) && (rigidbody.useGravity == false))
        {
            
            // Makes the item fall after 3 seconds
            Debug.Log("3 seconds have passed, now resetting timer.");
            rigidbody.useGravity = true;
            timer = 0;
        }
 //Here is where you'd get rid of the value assignment.
        if((timer >= 1.25) && (rigidbody.useGravity == true))
        {
            // Waits for the item to hit the ground and then resets it
            Debug.Log("1 seconds have passed, now resetting timer.");
            rigidbody.useGravity = false;
            timer = 0;
            transform.Translate(0,5,0);
        }
    }
}

If the boolean is not null-able or a three-state then you can also write it like this.

 ...
        if((timer >= 6) && !rigidbody.useGravity)
        {
 ...
  
 ...
        if((timer >= 1.25) && rigidbody.useGravity)
        {
 ...

Visual Studio Green Squiggles Warning article I was talking about in the comment.

Leave a Reply