Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Cannot implicitly content "bool" to "float"

Discussion in 'Scripting' started by TurtlePowderPow, Jan 13, 2018.

  1. TurtlePowderPow

    TurtlePowderPow

    Joined:
    Jan 13, 2018
    Posts:
    15
    So, I just got to using unity and scripting (that I really don't know about)
    So what I wanted to do, is that when I pick up an object the gravity goes off.

    But how can I get it to do so, that it turns the whole rigidbody off while I am holding down the mouse button? It keeps saying "Cannot implicitly content "bool" to "float""


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

    public class PickUp : MonoBehaviour
    {
    public Transform target;

    void OnMouseDown()
    {
    SetRigidbody(false);
    this.transform.position = target.position;
    SetGravity(false);
    this.transform.parent = GameObject.Find("FirstPersonCharacter").transform;
    }

    void OnMouseUp()
    {
    SetGravity(true);
    this.transform.parent = null;
    }

    private void SetRigidbody(bool j)
    {
    this.GetComponent<Rigidbody>().mass = j;
    }

    private void SetGravity(bool v)
    {
    this.GetComponent<Rigidbody>().useGravity = v;
    }
    }
     
  2. Indie_Dev

    Indie_Dev

    Joined:
    Dec 29, 2017
    Posts:
    35
    Because your SetRigidBody function is trying to enter a bool, for mass, which is a float value.... Try adding an if/then statement to your SetRigidbody function, that on false, sets the mass to 0, and on true, sets it back to its original mass. :)
     
    TurtlePowderPow likes this.
  3. TurtlePowderPow

    TurtlePowderPow

    Joined:
    Jan 13, 2018
    Posts:
    15
    Thanks, but... how exactly do I do that... I really don't know..
     
  4. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    Please look at this page for posting code on the forums: https://forum.unity.com/threads/using-code-tags-properly.143875/

    If you alter the method signature so it takes a float as a parameter, you could send the float, instead :)

    if you really had (want) to keep a bool, you could do this:
    Code (csharp):
    1.  
    2. private void SetRigidbody(bool j)
    3. {
    4. if(j == false)
    5.    this.GetComponent<Rigidbody>().mass = 0;
    6. else GetComponent<Rigidbody>().mass = 1; // or whatever it is supposed to be.
    7. }
     
    TurtlePowderPow and Indie_Dev like this.
  5. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
  6. Indie_Dev

    Indie_Dev

    Joined:
    Dec 29, 2017
    Posts:
    35
    First the quick and dirty answer:

    Code (CSharp):
    1. private void SetRigidbody(bool j)
    2. {
    3.      if (j == false) {
    4.           this.GetComponent<Rigidbody>().mass = 0.0f;
    5.      } else {
    6.           this.GetComponent<Rigidbody>().mass = <WHATEVER MASS IT'S SET AT INITIALLY>;
    7.     }
    8. }
    Or, the proper way to do this, if you're not going to want to ever set it to any values other than the starting mass and 0.0 mass, would be to add a variable to your class, outside of your function calls, and on your start method, set this to your mass. Then, when you run your SetRigidbody, just set it to that value. This would allow you to change your mass in the editor, without having to change your script.
    Code (CSharp):
    1. private float initialMass;
    2.  
    3. void Start()
    4. {
    5.      initialMass = this.GetComponent<Rigidbody>().mass;
    6. }
    7.  
    8. private void SetRigidbody(bool j)
    9. {
    10.      if (j == false) {
    11.           this.GetComponent<Rigidbody>().mass = 0.0f;
    12.      } else {
    13.           this.GetComponent<Rigidbody>().mass = initialMass;
    14.      }
    15. }
     
    TurtlePowderPow likes this.
  7. Indie_Dev

    Indie_Dev

    Joined:
    Dec 29, 2017
    Posts:
    35
    Sorry, I assumed the CODE tag would automatically inset bracket'ed code... just edited for clarity.
     
    TurtlePowderPow likes this.
  8. TurtlePowderPow

    TurtlePowderPow

    Joined:
    Jan 13, 2018
    Posts:
    15
    Sorry to go on and on BUT how do I make it turn the WHOLE idrigbody to turn off while the mouse is DOWN
    sorry for this... it didint solve the issue of why the pickup would not stop spinning when it was in my hand
     
  9. Indie_Dev

    Indie_Dev

    Joined:
    Dec 29, 2017
    Posts:
    35
    Code (CSharp):
    1.  
    2. private void StopSpinning()
    3. {
    4.      this.GetComponent<Rigidbody>().angularVelocity = new Vector3(0.0f, 0.0f, 0.0f);
    5. }
    6.  
    Calling that should remove any sort of rotation being done by the rigidbody.
     
    TurtlePowderPow likes this.
  10. Indie_Dev

    Indie_Dev

    Joined:
    Dec 29, 2017
    Posts:
    35
    FYI, you can also make it equal Vector3.zero. That's a shorthand version.
     
  11. TurtlePowderPow

    TurtlePowderPow

    Joined:
    Jan 13, 2018
    Posts:
    15
    says: No overload for method "StopSpinning" takes 1 arguments"

    am I just dumb..?
     
  12. Indie_Dev

    Indie_Dev

    Joined:
    Dec 29, 2017
    Posts:
    35
    I think you're trying to pass it in a value. Since StopSpinning doesn't have anything between the parantheses, you don't pass it any value
     
    TurtlePowderPow likes this.
  13. Indie_Dev

    Indie_Dev

    Joined:
    Dec 29, 2017
    Posts:
    35
    No. You're inexperienced. All of this comes with experience, time and practice.
     
  14. TurtlePowderPow

    TurtlePowderPow

    Joined:
    Jan 13, 2018
    Posts:
    15
    so I don't do anything to this?:
    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;

    public class PickUp : MonoBehaviour
    {
    public Transform target;

    void OnMouseDown()
    {
    this.transform.position = target.position;
    SetGravity(false);
    StopSpinning();
    this.transform.parent = GameObject.Find("FirstPersonCharacter").transform;
    }

    void OnMouseUp()
    {
    StopSpinning();
    SetGravity(true);
    this.transform.parent = null;
    }
    private void SetGravity(bool v)
    {
    this.GetComponent<Rigidbody>().useGravity = v;
    }
    private void StopSpinning()
    {
    this.GetComponent<Rigidbody>().angularVelocity = new Vector3(0.0f, 0.0f, 0.0f);
    }
    }
     
  15. TurtlePowderPow

    TurtlePowderPow

    Joined:
    Jan 13, 2018
    Posts:
    15
    WAIT. It works. BUT it keeps sliding. Stopped spinning, but keeps sliding. How to stop the sliding?
     
  16. TurtlePowderPow

    TurtlePowderPow

    Joined:
    Jan 13, 2018
    Posts:
    15
    Actually I will upload a video so you will understand whats wrong better if this sounds confusing
     
  17. TurtlePowderPow

    TurtlePowderPow

    Joined:
    Jan 13, 2018
    Posts:
    15
    here it is.
    mic is kinda low
     
  18. Indie_Dev

    Indie_Dev

    Joined:
    Dec 29, 2017
    Posts:
    35
    OH! Okay, so you need to set both the "velocity" AND "angularVelocity" to Vector3.zero in order to prevent that from happening. :)
     
  19. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    Maybe just make it kinematic when you pick it up.

    Yes, or set the velocity to zero, too.

    I'm not sure you'd want a dynamic rb as you walk around though, but who knows.
     
  20. TurtlePowderPow

    TurtlePowderPow

    Joined:
    Jan 13, 2018
    Posts:
    15
    Last thing, I promise...

    Just how do I fix this issue. Video will tell whats wrong
     
  21. TurtlePowderPow

    TurtlePowderPow

    Joined:
    Jan 13, 2018
    Posts:
    15
    meaby rb.detectcollisions might work if I know how to use it
     
  22. Indie_Dev

    Indie_Dev

    Joined:
    Dec 29, 2017
    Posts:
    35
    In that case, you'd want to follow methos5k's suggestion.

    When you pick it up
    Code (csharp):
    1.  
    2. GetComponent<Rigidbody>().isKinematic = true;
    3.  
    When you drop it
    Code (csharp):
    1.  
    2. GetComponent<Rigidbody>().isKinematic = false;
    3.  
    This will make it immune to physics while you're holding it.


    Pro-Tip:
    In they object's start function, create a reference to rigidbody, so you don't have to run GetComponent<Rigidbody>() all the time. It will make it quicker. :) I'm not gonna tell you how to do that one though. :p Practice! ^_^
     
  23. TurtlePowderPow

    TurtlePowderPow

    Joined:
    Jan 13, 2018
    Posts:
    15
    FINALLY GOT IT WORKING THANK YOU EVERYONE WHO HELPED :DDDDDDDD

    This was it:
    public Rigidbody rb;
    void Start()
    {
    rb = GetComponent<Rigidbody>();
    }
     
    Indie_Dev likes this.
  24. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    Glad you got it working. :)
     
    TurtlePowderPow likes this.