Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 /
avatar image
0
Question by mikavm · Jun 30, 2015 at 06:42 PM · c#spawngetcomponentcomponentenabled

C# GetComponent, component not turning off.

Hello, I'm trying to enable the script 'Rigidbody First Person Controller' using c#. This is my code...

     public void SpawnMyPlayer() {
         Spawnpoint mySpawnSpot = spawnPoint[Random.Range (0, spawnPoint.Length)];
         GameObject myPlayerGO = (GameObject) PhotonNetwork.Instantiate("Player", mySpawnSpot.transform.position, mySpawnSpot.transform.rotation, 0);
         SpectateCam.enabled = false;
         ((MonoBehaviour)myPlayerGO.GetComponent("Rigidbody First Person Controller")).enabled = true;
 
         /*Component comp = myPlayerGO.gameObject.GetComponent("RigidbodyFirstPersonController");
         comp.gameObject.SetActive(true);*/
     }
 }
 

Thanks!

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
Best Answer

Answer by Hellium · Jun 30, 2015 at 06:46 PM

The function Transform.Find is not able to get inactive GameObjects.

GetComponent works the same way. If the component is not enabled, GetComponent won't be able to get it.

Anyway, make sure the name of your component is correct. Don't rely on the name in the inspector but the name of the script itself :

 (myPlayerGO.GetComponent<RigidbodyFirstPersonController>()).enabled = true;

Make sure you the RigidbodyFirstPersonController is defined in the same namespace of your script. If not add the following line at the top of your script :

 using <NameOfTheNamespaceOfRigidbodyFirstPersonController>
Comment
Add comment · Show 6 · 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 mikavm · Jun 30, 2015 at 07:00 PM 0
Share

I made sure the name is the exact name of the script, however I get this error: Assets/$$anonymous$$ultiplayer/SpawnPlayer.cs(18,42): error CS0246: The type or namespace name `RigidbodyFirstPersonController' could not be found. Are you missing a using directive or an assembly reference?

avatar image Hellium · Jun 30, 2015 at 07:17 PM 0
Share

Does the RigidbodyFirstPersonController is defined inside a namespace ?

If so, add using <TheNameOfTheNameSpace> at the top of your SpawnPlayer script.

Could we take a look at the RigidbodyFirstPersonController.cs file ?

avatar image mikavm · Jun 30, 2015 at 07:22 PM 0
Share

From what I know it isn't defined inside a namespace, though here's the RigidbodyFirstPersonController.cs (It's from the Unity standard assets): http://pastebin.com/6AbFAvGA

avatar image Hellium · Jun 30, 2015 at 07:25 PM 0
Share

Bingo !

 namespace UnityStandardAssets.Characters.FirstPerson

Then, add using UnityStandardAssets.Characters.FirstPerson at the beginning of your SpawnPlayer script.

avatar image mikavm · Jun 30, 2015 at 07:26 PM 0
Share

It works! Thank you very much!!

Show more comments
avatar image
1

Answer by Pindwin · Jun 30, 2015 at 06:48 PM

Considering your script exact name is "RigidbodyFirstPersonController.cs", which means component being called "RigidbodyFirstPersonController", try

 myPlayerGO.GetComponent<RigidbodyFirstPersonController>().enabled = true;

By the way, such code may throw an exception, which leads to bit more proper code:

 try{
     myPlayerGO.GetComponent<RigidbodyFirstPersonController>().enabled = true;
 }catch(System.NullReferenceException e){
     //serve your exception here.
 }

I hope it helps.

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 mikavm · Jun 30, 2015 at 06:59 PM 0
Share

I got this error: Assets/$$anonymous$$ultiplayer/SpawnPlayer.cs(18,41): error CS0246: The type or namespace name `RigidbodyFirstPersonController' could not be found. Are you missing a using directive or an assembly reference?

avatar image Pindwin · Jul 01, 2015 at 01:15 PM 0
Share

sorry couldn't reply, was offline since answering. Glad to see you got this sorted out :)

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Disabling scripts 1 Answer

Adjusting size of a halo in C# 0 Answers

enabled is not a member of UnityEngine.Component 1 Answer

Disabled script still working C# 3 Answers

Multiple Cars 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