Search Unity

Disable Script Component in C#

Discussion in 'Editor & General Support' started by Alianos, Jun 22, 2017.

  1. Alianos

    Alianos

    Joined:
    Jun 21, 2017
    Posts:
    5
    Hello,

    i have a little Problem. All what i want to do is to disable a Script Component.

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.SceneManagement;
    5. using UnityEngine.UI;
    6.  
    7. public class Resp : MonoBehaviour {
    8.  
    9.  
    10.     public Transform respawnLocation;
    11.     static bool playerIsDead = false;
    12.     public Button buttonRespawn;
    13.     public Button buttonMainMenu;
    14.     public Button buttonQuitGame;
    15.     Component freezeAttacking;
    16.    Component  freezePlayer;
    17.  
    18.     // Use this for initialization
    19.      void Start () {
    20.  
    21.         freezePlayer = GetComponent("FirstPersonController");
    22.         freezeAttacking = GameObject.Find("FirstPersonCharacter").GetComponent("MeleeSystem");
    23.         var weapon = GameObject.Find("Axe").GetComponent("MeshRenderer");
    24.  
    25.     }
    26.  
    27.     // Update is called once per frame
    28.     void Update () {
    29.         if (playerIsDead == true)
    30.         {
    31.             freezePlayer.enabled = false;
    32.  
    33.             buttonRespawn.gameObject.SetActive(true);
    34.             buttonMainMenu.gameObject.SetActive(true);
    35.             buttonQuitGame.gameObject.SetActive(true);
    36.  
    37.             Cursor.visible = true;
    38.             Cursor.lockState = CursorLockMode.None;
    39.         }
    40.     }
    41. }

    i Get the Error Code CS1061 wich say
    Code (CSharp):
    1. Assets/Scripts/Resp.cs(31,26): error CS1061: Type `UnityEngine.Component' does not contain a definition for `enabled' and no extension method `enabled' of type `UnityEngine.Component' could be found. Are you missing an assembly reference?"
    Thank you very much
     
    Last edited: Jun 22, 2017
  2. fffMalzbier

    fffMalzbier

    Joined:
    Jun 14, 2011
    Posts:
    3,276
    Why is the Variable freezePlayer of type component if you storing a FirstPersonController in it?
    The class FirstPersonController does have the property enabled but the class Component does not.
     
  3. Alianos

    Alianos

    Joined:
    Jun 21, 2017
    Posts:
    5
    do you know how i can solve this problem ?
     
  4. aer0ace

    aer0ace

    Joined:
    May 11, 2012
    Posts:
    1,513
    @Alianos Try to "connect the dots" here (because that's what you'll be doing for the rest of your game development life).

    What does this tell you about your variable freezePlayer?

    Since you say FirstPersonController is a script, the script is derived from type Monobehaviour, right?

    https://docs.unity3d.com/ScriptReference/MonoBehaviour.html
    Which inherits from Behaviour
    https://docs.unity3d.com/ScriptReference/Behaviour.html
    Which inherits from Component.
    If you look at the Behaviour docs, you will see that enabled is a property.
    https://docs.unity3d.com/ScriptReference/Behaviour-enabled.html

    Does this help?
     
  5. Alianos

    Alianos

    Joined:
    Jun 21, 2017
    Posts:
    5
    first thank you for your answer. Yes i Know that enabled is a property. And now i Know also that the property "enabled" isnt in the Component class. But i dont know how i can solve my problem..
     
  6. aer0ace

    aer0ace

    Joined:
    May 11, 2012
    Posts:
    1,513
  7. Alianos

    Alianos

    Joined:
    Jun 21, 2017
    Posts:
    5
    i read it but i did not understand how i use it in my case
     
  8. aer0ace

    aer0ace

    Joined:
    May 11, 2012
    Posts:
    1,513
    How about inheritance? Are you familiar with inheritance?
    What have you tried so far?
     
  9. Alianos

    Alianos

    Joined:
    Jun 21, 2017
    Posts:
    5
    I have this java Code:
    Code (JavaScript):
    1. var freezePlayer;
    2. var freezeAttacking;
    3. var weapon;
    4. var respawnLocation : Transform;
    5. static var playerIsDead = false;
    6.  
    7. var buttonRespawn : Button;
    8. var buttonMainMenu : Button;
    9. var buttonQuitGame : Button;
    10.  
    11. function Start ()
    12. {
    13.     freezePlayer = GetComponent("FirstPersonController");
    14.     freezeAttacking = GameObject.Find("FirstPersonCharacter").GetComponent("MeleeSystem");
    15.     weapon = GameObject.Find("Axe").GetComponent("MeshRenderer");
    16. }
    17.  
    18. function Update ()
    19. {
    20.     if (playerIsDead == true)
    21.     {
    22.         freezePlayer.enabled = false;
    23.         freezeAttacking.enabled = false;
    24.         weapon.enabled = false;
    25.        
    26.         buttonRespawn.gameObject.SetActive(true);
    27.         buttonMainMenu.gameObject.SetActive(true);
    28.         buttonQuitGame.gameObject.SetActive(true);
    29.        
    30.         Cursor.visible = true;
    31.         Cursor.lockState = CursorLockMode.None;
    32.     }
    33. }

    i want to translate this Code to C# Code. that is all.
     
  10. xVergilx

    xVergilx

    Joined:
    Dec 22, 2014
    Posts:
    3,296
    Tips:
    1. Use a proper type for freezeAttacking/freezePlayer instead of Component; You can always try to cast Component (with direct casts like "(FirstPersonController)" or safe casts like "as FirstPersonController" to the required type, but it's much simpler and more usefull to keep a proper type instead of generic.
    2. Don't use GetComponent("string"); It sucks. Use typed GetComponent<yourtypehere>(). It already casts and returns a proper type for the component.
    3. GameObject.Find is a slow operation. Use direct references either from inspector or using tags/other components search;
    4. Learn C# and inheritance overall.
     
    aer0ace and fffMalzbier like this.