Click here to Skip to main content
15,888,527 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Here's the script I have on Unity


C#
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class PlayerMovement : MonoBehaviour
{
    
    public float speed;
    public Text ChestText;
    
    Animator anim;
    public bool CurrentChest;
    public bool OpenedChest;
    Vector2 lookDirection = new Vector2(1, 0);
    private Rigidbody2D rb2d;

    void Start()

    {
        rb2d = GetComponent<rigidbody2d>();
        anim = GetComponent<animator>();
        CurrentChest = (true);
        ChestText.text = "";
    
    }

    // Update is called once per frame
    void Update()
    {
        float horizontal = Input.GetAxis("Horizontal");
        float vertical = Input.GetAxis("Vertical");

        Vector2 move = new Vector2(horizontal, vertical);

        if (!Mathf.Approximately(move.x, 0.0f) || !Mathf.Approximately(move.y, 0.0f))
        {

            lookDirection.Set(move.x, move.y);
            lookDirection.Normalize();
        }


        anim.SetFloat("Look X", lookDirection.x);
        anim.SetFloat("Look Y", lookDirection.y);
        anim.SetFloat("Speed", move.magnitude);

        Vector2 position = rb2d.position;
        position = position + move * speed * Time.deltaTime;
        rb2d.MovePosition(position);

    }
    void OnTriggerEnter2D(Collider2D other)
    {
        if (other.gameObject.CompareTag("Chest"))
        {
            ChestText.text = "Press 'E' to open";
            if (Input.GetKeyDown("e"))
            {
                CurrentChest = false;
                OpenedChest = true;
            }
        }

        void OnTriggerExit()
        {
            ChestText.text = false;
        }
    }
   
}


What I have tried:

>Sorry if this looks really bad, I started programming really recently.
Posted
Updated 24-Nov-19 18:29pm
v2
Comments
Celine Ribaya BSIT 2 10-Mar-22 22:43pm    
Hello, how about string to int?

1 solution

The problem is here:
C#
void OnTriggerExit()
{
    ChestText.text = false;
}
The error says that you are trying to store a value of type boolean, in a variable (property, field, member...) that expects a string value.

Luckily, in C# you do a ToString() function call and convert the data to string. So, change your function to:
C#
void OnTriggerExit()
{
    ChestText.text = false.ToString(); 
}
But again, what is the point of this? Maybe you wanted to modify CurrentChest?

boolean to string. | C# Online Compiler | .NET Fiddle[^]
 
Share this answer
 

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