Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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 :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading