Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
Question by Nicolaiaiai · Nov 18, 2016 at 04:40 PM · getcomponentenabledenable and disable script

Disable a Component with a script. Not working

I tried to disable the Script "FirstPersonController" on the FPSController, "MeleeSystem" on FirstPersonCharacter and "MeshRenderer" on Axe. Here you can see the hierarchy:

alt text

Here you can see my Code:

 public class RespawnSystem : MonoBehaviour
 {
     Component freezePlayer;
     Component freezeAttacking;
     Component weapon;
     public Transform respawnLocation;
     public static bool playerIsDead = false;
 
     public Button buttonRespawn;
     public Button buttonMainMenu;
     public Button buttonQuitGame;
 
     void Start()
     {
         freezePlayer = GetComponent("FirstPersonController");
         freezeAttacking = GameObject.Find("FirstPersonCharacter").GetComponent("MeleeSystem");
         weapon = GameObject.Find("Axe").GetComponent("MeshRenderer");
     }
 
     void Update()
     {
         if (playerIsDead == true)
         {
             // freezePlayer.enabled = false
             // freezeAttacking.enabled = false
             // weapon.enabled = false;
 
             buttonRespawn.gameObject.SetActive(true);
             buttonMainMenu.gameObject.SetActive(true);
             buttonQuitGame.gameObject.SetActive(true);
             Cursor.visible = true;
             Cursor.lockState = CursorLockMode.None;
         }
     }

The Lines with the Comment are not working.

Error: Error 2 "UnityEngine.Component" does not contain a definition for "enabled" and could not find an extension method "enabled" that accepts a first UnityEngine.Component argument.

Thank you for the help! Nicolai

I am really sorry for my bad English!

asda.png (34.7 kB)
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by Cepheid · Nov 18, 2016 at 04:48 PM

@Nicolaiaiai

When you are trying to cache components as variables it's better to use the actual component class as the datatype rather than using the base class Component. This is because the component base class does not have a variable for enabling and disabling components.

So, if you want to get the FirstPersonController component, then specify the FirstPersonController as the datatype. For example, you would change your variables to the following:

 public class RespawnSystem : MonoBehaviour
  {
        FirstPersonController freezePlayer;
        MeleeSystem freezeAttacking;
        MeshRenderer weapon;
 
       // ...
 }

Also, when getting components, it's often better to use the generic way. So, all together, your class should now look like:

 public class RespawnSystem : MonoBehaviour
  {
      FirstPersonController freezePlayer;
      MeleeSystem freezeAttacking;
      MeshRenderer weapon;
      public Transform respawnLocation;
      public static bool playerIsDead = false;
  
      public Button buttonRespawn;
      public Button buttonMainMenu;
      public Button buttonQuitGame;
  
      void Start()
      {
          freezePlayer = GetComponent<FirstPersonController> ();
          freezeAttacking = GameObject.Find("FirstPersonCharacter").GetComponent<MeleeSystem> ();
          weapon = GameObject.Find("Axe").GetComponent<MeshRenderer> ();
      }
  
      void Update()
      {
          if (playerIsDead == true)
          {
              freezePlayer.enabled = false
              freezeAttacking.enabled = false
              weapon.enabled = false;
  
              buttonRespawn.gameObject.SetActive(true);
              buttonMainMenu.gameObject.SetActive(true);
              buttonQuitGame.gameObject.SetActive(true);
              Cursor.visible = true;
              Cursor.lockState = CursorLockMode.None;
          }
      }
 }
Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Nicolaiaiai · Nov 18, 2016 at 06:20 PM 0
Share

I get an Error on line 3 and 15. The Error said: The type or namespacename "FirstPersonController" could not be found. $$anonymous$$eleeSystem and weapon is working though.

avatar image Cepheid Nicolaiaiai · Nov 19, 2016 at 01:11 AM 0
Share

@Nicolaiaiai

Ah yes, that's because Unity places it's FirstPersonController under a seperate namespace, I forgot about that. Simply add the following line to the top of the file:

 using UnityStandardAssets.Characters.FirstPerson

That should fix it.

avatar image
0

Answer by kami1339 · Mar 28, 2019 at 01:02 PM

use this

 public GameObject otherobj;//your other object
     public string scr;// your secound script name
     void Start () {
     (otherobj. GetComponent(scr) as MonoBehaviour).enabled = false;
     }

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

NullReferenceException: Object reference not set to an instance of an object 0 Answers

Reference a script on a gameobject that is disabled in scene 0 Answers

"NullReferenceException: Object reference not set to an instance of an object " only for one component 2 Answers

Referencing a function in a C# script 1 Answer

GetComponent not working 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges