Home > Mobile >  Setting a bool active from a different script
Setting a bool active from a different script

Time:06-08

I need to active icons in my UI. Since I make use of a pick up script I want to activate the object once I picked it up. I made a bool in my main script but I can't turn the bool on or off in my pick up script.

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

public class NadePickUp : MonoBehaviour
{

    public GameObject PUEffect;
    
    private void OnTriggerEnter2D(Collider2D other)
    {
        if (other.CompareTag("Player"))
        {
            PickUp(other);
            // activate = true;
        }
    }

    void PickUp(Collider2D player)
    {
        Shooting stats = player.GetComponent<Shooting>();
        stats.shotType = "grenade";
        GameObject effect = Instantiate(PUEffect, transform.position, Quaternion.identity);    


        PlayerStats activate = player.GetComponent<PlayerStats>();
        activate = true;

        Destroy(gameObject);
    }
}

CodePudding user response:

There are many answers to something like this on Unity Answers, here for example is one that answers your question.

If your PlayerStats is a class, you're treating it as a boolean. You're getting a reference to the script component and then immediately setting that value to true.

Assuming there's a public boolean activate on your PlayerStats class you could do something like this:

PlayerStats playerStats = player.GetComponent<PlayerStats>();
playerStats.activate = true;

This is based on pure assumption of your PlayerStats class.

  • Related