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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Disabling a script on a game object using C#

Discussion in 'Scripting' started by jevanswow, May 30, 2015.

  1. jevanswow

    jevanswow

    Joined:
    Apr 17, 2015
    Posts:
    33
    Hi all

    I am trying to code a way that the mouse and keyboard (code is off the first person controller script) gets disabled when I'm doing functions such as saving/loading and checking character stats. For example when I press c the game will show the player class and player rank and then I want it to disable the first person controller script so nothing else gets shown until I click on the ok button.

    I did follow along with the light example in the tutorials and that works perfect however when I'm doing a Component then a GetComponent it's saying that UnityEngine.Component does not contain a definition for enabled. Also I did try to change the variable in the firstpersoncontroller script to false but for some reason it's not finding it and both the script and the variable is public. Here's the code.

    using UnityEngine;
    using System.Collections;

    public class playerstats : MonoBehaviour
    {
    public static string Class = "None";
    public static string Rank = "None";
    public static string weapon = "None";
    public static string accessory = "None";
    bool characterstats = false;
    public string [] Acknowledge;
    bool equipment = false;
    bool DisplayDialog = false;
    bool Confirmed = false;
    public static int gold = 0;
    public Component movement;

    void Start()

    {
    movement = GetComponent<FirstPersonController>();
    }

    void Update ()

    {
    if (Input.GetKeyUp (KeyCode.C))

    {
    movement.enabled = !movement.enabled;
    characterstats = true;
    Confirmed = true;

    }


    if (Input.GetKeyUp (KeyCode.E))

    {
    equipment = true;
    }
    }

    void OnGUI()

    {
    GUI.color = Color.white;
    GUILayout.BeginArea (new Rect (0, 0, 300, 300));

    if (characterstats == true)

    {

    GUILayout.Label ("Character Data:");
    GUILayout.Label ("Class:" + " " + Class);
    GUILayout.Label ("Rank:" + " " + Rank);

    if (GUILayout.Button (Acknowledge [0]))

    {
    characterstats = false;
    Confirmed = false;
    }
    }

    if (equipment == true)

    {
    GUILayout.Label ("Equipment:");
    GUILayout.Label ("Weapon:" + " " + weapon);
    GUILayout.Label ("Accessories:" + " " + accessory);

    if (GUILayout.Button (Acknowledge [0]))

    {
    equipment = false;
    }
    }

    GUILayout.EndArea ();
    }
    }

    I'm still learning C# so I apologize if I don't get the technical term right but I know what I need is to find the right function that has enabled part of it (like the light function has the enabled in it) but unfortunately I just don't know which one of those I need to go along with what I'm trying to do.

    Also another problem I'm having is that the component on the player is actually called First Person Controller and not FirstPersonController so if I try to space it I also have problems.

    Any help on this would certainly be appreciated.

    Thanks
     
  2. RiokuTheSlayer

    RiokuTheSlayer

    Joined:
    Aug 22, 2013
    Posts:
    356
    Use Code Tags so everyone can read your code correctly, please.
     
  3. jevanswow

    jevanswow

    Joined:
    Apr 17, 2015
    Posts:
    33
    I'm sorry as I'm still new and learning all the technical terms I don't know what you mean by code tags but pretty much what I'm trying to do is make a way to disable the script that controls player movement and mouse movement.

    For example when a player presses c the character stats will show and will continue to show until the ok button is clicked on. Unfortunately the problem I'm having is that the player can still move, open up the save/load/exit and other gui functions while the character stats gui is still showing and that's a problem.

    As I said on my previous post I followed the light example but as the script isn't part of the light I just can't do the private Light movement, so if anyone knows what I should replace Light with so it'll have enabled that's all I need.

    Thanks
     
  4. RiokuTheSlayer

    RiokuTheSlayer

    Joined:
    Aug 22, 2013
    Posts:
    356
  5. MD_Reptile

    MD_Reptile

    Joined:
    Jan 19, 2012
    Posts:
    2,663
    Step one is to determine where player input is taken (I didn't even read the script lol) and wrap it in a statement like "if (!DontAcceptInput){ // movement input here }"

    And if you need to disable player input, then set "DontAcceptInput" to true (bool variable) and it won't let them move!
     
  6. jevanswow

    jevanswow

    Joined:
    Apr 17, 2015
    Posts:
    33
    Thank you very much that's definitely a good start and I appreciate it. I am definitely learning more than when I first started and I don't mean to sound like a noob or even like I'm just coming here for answers, the problem I'm facing is that the stuff I need to look, I just can't find doing Yahoo searches or youtube searches so I appreciate all the help I can get.
     
  7. MD_Reptile

    MD_Reptile

    Joined:
    Jan 19, 2012
    Posts:
    2,663
    Google is almighty. No seriously forget yahoo and google search everything, I find that it gets me what I need every time almost.

    Edit: disclaimer - I've used yahoo mail and other services for more than a decade - just the facts are that Google has the upper hand in search algorithms - not that I dislike yahoo at all haha.