Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

How to disable a script from the player gameObject

So i tried to disable the PlayerConfiguration Script with this line of code: player = GameObject.FindGameObjectWithTag("Player").GetComponent<PlayerConfiguration>().gameObject.SetActive(true);

But i got this error

i searched on google but nothing

pls help

«1

Comments

  • Try GameObject with a lower case g

  • edited May 21


    another @spiderManAW

  • Does it’s know what gameobject is?

  • Have you specified

  • that's why i used FindGamObjectWithTag

    to find the gameobject

    and it's only one game object with that tag

  • SanderSander Member

    What is the type of your player variable? You probably need to get the component stored in a variable

  • This is the entire code:

    using System.Collections;

    using System.Collections.Generic;

    using UnityEngine;

    using UnityEngine.UI;


    public class HealthBar : MonoBehaviour

    {

    public Slider PlayerSlider;

    public Slider ProgramSlider;

    public WinOrLose Screen;

    GameObject player;


    void Start()

    {

    player = gameObject.FindGameObjectWithTag("Player").GetComponent<PlayerConfiguration>().gameObject.SetActive(true);

    Debug.Log(player);

    }


    public void SetMaxValue(float mValue)

    {

    PlayerSlider.maxValue = mValue;

    PlayerSlider.value = mValue;

    }


    public void SetValue(float Value)

    {

    PlayerSlider.value = Value;

    }


    public void PrSetMaxValue(float mValue)

    {

    ProgramSlider.maxValue = mValue;

    ProgramSlider.value = mValue;

    }


    public void PrSetValue(float Value)

    {

    ProgramSlider.value = Value;

    }


    void Update()

    {

    if (PlayerSlider.value <= 0)

    {

    player.SetActive(false);

    Screen.LoseMenu(0);

    }

    else if (ProgramSlider.value <= 0)

    {

    player.SetActive(false);

    Screen.LoseMenu(1);

    }


    }

    }

  • Have you tagged your player properly with right cases?

  • SanderSander Member
    edited May 21

    You can't do what you're trying to do.

    First find the player gameobject by writing: player = gameObject.FindGameObjectWithTag("Player");

    Then you can go: player.GetComponent<PlayerConfiguration>().SetActive(true);

    What you tried to do was assigning your player variable to the return of SetActive, which is null, therefore the error.

  • ok

    leet me try

  • Change GameObject player to PlayerConfiguration player


    Then say player=GameObject.FindGameObjectWithTag("Player").GetComponent<PlayerConfiguration>().enabled=false

  • SanderSander Member

    @TumiTheSoleMaker

    Pretty sure that won't work either, as you shouldn't assign the variable when doing this.

  • i think it's enabled = false;

  • SanderSander Member

    Yea you need to go: player.GetComponent<PlayerConfiguration>().enabled = true/false;

    Couldn't remember how SetActive worked.

  • woorked!!

    thannks @Sander

  • SanderSander Member

    No problem my guy :)

  • one more question @Sander

    can i disable one script from more than one enemy at once?

    like to disable the EnemyConfiguration from all enemies

  • SundarSundar Member

    I have a idea....set a static bool variable and use an if statement to make sure that the script is executed only if the bool is true... initially set it to true...later u can disable it from other script..as it is static u can call it and change the value...or else you can also use a static integer and maybe set to any number like one or something.... but the script shuldnt execute if value isnt 1(in my example),..make sure u make it static...

  • SanderSander Member

    Hmmm, I'd probably do something like:

    Get all enemies into an array or list and then:

    foreach(GameObject enemy in ArrayOfEnemies){

    enemy.getComponent<NameOfScript>().enabled = false/true;

    }

  • edited May 21

    :///

    @Sundar

    you lost me from the first static word

    i can't do that

    i am still a begginer

  • SetActive enables and/or disables *GameObjects*

  • hmm

    @Sander that sounds good

  • @Sander Can i put them in an array of gameobj?

  • i tried to put them in the array like this:

    enemies = GameObject.FindGameObjectsWithTag("Enemy");

    @Sander

  • SanderSander Member
    edited May 21

    Yea, you can use something along the lines of:

    first declare your array and then:

    array = GameObject.FindGameObjectsWithTag("Enemy");

  • SanderSander Member

    It didn't work, or?

  • SundarSundar Member
    edited May 22

    See as I had mentioned earlier you use a static variable...let me explain....

    In Player configuration script, create a public variable like this...

    public static int a=1;

    Then make an if statement which shuld make sure that the script is executed only when a is equal to one

    if(a==1)

    Add this wherever necessary..then u got to another script. frm where u will enable and disable player config script frm executing..

    In your case it is the HealthBar script


    So whenever u want to disable the script...

    Write this:

    PlayerConfiguration.a=-1;

    A could be assigned to any value other than one here in this case...

    Hence the if statement in PlayerConfiguration becomes false..

    Hence the code in the script is not executed... only thing is to make sure u place the if statement correctly

    Hope this works..

  • SundarSundar Member


Sign In or Register to comment.