Click here to Skip to main content
14,691,895 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
My problem is that it gives me this error:
Assets\Collision.cs(11,28): error CS0029: Cannot implicitly convert type 'bool' to 'PlayerMovement`




Here is my code: 




<pre>using UnityEngine;

public class Collision : MonoBehaviour
{
    public PlayerMovement playerMovement;
    void OnCollisionEnter(Collision collisionInfo)
    {
        if (GetComponent<collider>().tag == "Ded")
        {
          playerMovement = false;
        }

    }






Thank you!

What I have tried:

Im following Brackeys tutorial.
Posted
Updated 19-Oct-20 23:21pm

There are a number of problems here - one of which Richard has described.

The error message is saying that it can't convert a bool value - false - a PlayerMovement, so you can't assign false to the variable playerMovement - in the same way as you can't put a car into a coffee cup: they are unrelated items, and just don't fit together!

Probably, what you wanted to do was set a property of the PlayerMovement class to false:
playerMovement.MyBoolProperty = false;

But in this specific case that will give you another error - a null reference exception at run time because you don't assign any value to the playerMovement variable, all you do is declare it:
public PlayerMovement playerMovement;
Somewhere in your code for that class, you need to create an instance of the PlayerMovement class by using the new keyword and assign it to the variable. The simplest way is like this:
public PlayerMovement playerMovement = new PlayerMovement();
.
   
The variable playerMovement is an object of type PlayerMovement which is (presumably) not a simple bool type, so you cannot set it to a bool value. If that does not make sense then I suggest you get hold of a book on C# and start learning.
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900