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 /
This question was closed Feb 04, 2017 at 11:12 AM by $$anonymous$$ for the following reason:

The question is answered, right answer was accepted

avatar image
1
Question by $$anonymous$$ · Sep 30, 2016 at 01:44 PM · c#character controllerenable and disable script

(Solved) Enabling a script from another script

I'm making a game with two controllable characters. The characters are both in the same area and when I press a button, I want the player to take control of the other character and leave the other where it was left. I have both characters as separate objects with their own controller script but I have trouble getting them work as I want to.

What I've been trying to do is has CharacterController1 enabled at the start of the game and when I press Q, the CC1 disables and CC2 enables. My CC1 to CC2 switch looks like this:

     public CharacterController1 ascript;
 public CharacterController2 bscript;
 
 void Start () {

     ascript = GetComponent<CharacterController1> ();
     bscript = GetComponent<CharacterController2> ();
 }

 void Update () 
 {
     if (Input.GetKeyDown (KeyCode.Q)) 
     {
         ascript.enabled = !ascript.enabled;
         bscript.enabled = bscript.enabled;
     }
 }

The CC2 has a similar script but the Update part is the opposite way. The problem I run to is that when I press play both of my CCs are enabled and so I control both character. When I press Q CC1 disables like it should but I can't get the CC2 to disable when I only want to control Character1.

I've tried adding lines like:

if (ascript.enabled) { bscript.enabled = !bscript.enabled; }

or

void Start() { bscript.enabled = !bscript.enabled; }

To the CC2 script but I can't get it to work.

Also I keep getting this Error message: NullReferenceException: Object reference not set to an instance of an object.

I have dragged the scripts to the inspector spaces made by the public CC1 and public CC2 and didn't have a problem there but when I press play and looks at Character1 for example the CC2 has disappeared from there. I have never done coding before and I have been googling and trying different things for hours but can't figure out what I'm doing wrong.

Comment
Add comment · Show 2
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 gjf · Sep 29, 2016 at 03:53 PM 0
Share

you need to include the complete error message and the script it references (unless it's really long) so we can see exactly which line produces the error.

for reference, GetComponent will get the component from the current GameObject. if you want to get it from a different object, you'll need to be more explicit. something like:

 someOtherComponentReference = someOtherObject.GetComponent<SomeOtherComponent>();

if you're new to coding, i'd suggest going through some of the unity tutorials until you get the hang of things.

avatar image $$anonymous$$ gjf · Sep 30, 2016 at 08:24 AM 0
Share

Didn't get that to work but I did some more googling and tried different things and managed to get rid of the error. $$anonymous$$y scripts look like this now:

public class CC1toCC2 : $$anonymous$$onoBehaviour {

 public CharacterController1 ascript;
 public CharacterCOntroller2 bscript;

 void Start () {

     ascript = GetComponent<CharacterController1> ();
     CharacterController2 bscript = GetComponent<CharacterController2> ();
 }

 void Update () 
 {
     if (Input.Get$$anonymous$$eyDown ($$anonymous$$eyCode.Q)) 
     {
         ascript.enabled = !ascript.enabled;
         bscript.enabled = bscript.enabled;
     }

     if (ascript.enabled)
     {
         bscript.enabled = !bscript.enabled;
     }
 }

}

and

public class CC2toCC1 : $$anonymous$$onoBehaviour {

 public CharacterController1 ascript;
 public CharacterCOntroller2 bscript;

 void Start () {

     CharacterController1 ascipt = GetComponent<CharacterController1> ();
     bscript = GetComponent<CharacterController2> ();
 }

 void Update () 
 {
     if (Input.Get$$anonymous$$eyDown ($$anonymous$$eyCode.Q)) 
     {
         ascript.enabled = ascript.enabled;
         bscript.enabled = !bscript.enabled;
     }
 }

}

This is how close I've gotten to get this to work: CC1 works perfectly and it disables and enabled fine like it's supposed to. But CC2 only works about 30% of the time. When CC1 is enabled and working, the tick on CC2 flashed on the inspector (I can't control the C2 though so that's not really a problem?) but when I try to switch to C2 the flashing usually stops and the script stays unticked. 30% of the time it goes ticked like it should. I need to switch back to C1 a couple of times to get it to work.

I've gone through all the scripting tutorials for beginners on Unity's website and I feel like I understand how things work but as soon as I start actually doing something nothing seems to work and I don't understand why. I've tried to find similar situations here and try thing that worked for those but I just can't get things to work. :/

2 Replies

  • Sort: 
avatar image
7
Best Answer

Answer by Halleester · Sep 30, 2016 at 02:07 PM

Before I answer: Instead of doing script.enabled = script.enabled; with both a and b script, try setting it equal to true or false, like this: script.enabled = true;. It will be a lot less confusing later on. Back to the question: What you are trying to do seems a lot more convoluted than it should be, and the scripts are probably both trying to swap at the same time, creating a loop between the two functions swapping scripts on and off, creating a race for which script calls the button press last. You could simplify this to one simple script:

  public CharacterController1 ascript;
  public CharacterController2 bscript;
  
  void Start () {
      ascript = GetComponent<CharacterController1> ();
      bscript = GetComponent<CharacterController2> ();
      ascript.enabled = true;
      bscript.enabled = false;
  }
  void Update () 
  {
      if (Input.GetKeyDown (KeyCode.Q)) 
      {
         if(ascript.enabled == true)
         {
           ascript.enabled = false;
           bscript.enabled = true;
         }
         else
         {
           ascript.enabled = true;
           bscript.enabled = false;
         }
      }
  }

This should work, and works inside one script. I didn't test it, so let me know if it has an error.

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 $$anonymous$$ · Sep 30, 2016 at 02:24 PM 0
Share

Thank you! That works perfectly.

I tried using true and false at one point but I was unable to work them so I could switch between more than once. Apparently I just did something wrong with that too.

But thank you, this was a huge help.

avatar image Halleester $$anonymous$$ · Sep 30, 2016 at 05:27 PM 0
Share

Any time! If you mark my question as correct, then it will automatically put it into the solved category so other people won't try to answer your question as well, and works better than putting"solved" in the title. :D

avatar image
0

Answer by Dak28 · Jan 12, 2017 at 05:03 PM

It dosen't work for me it give me this error: NullReferenceException: Object reference not set to an instance of an object StartGame.Update () (at Assets/Scripts/StartGame.cs:53)

but i have the script Catapult

Comment
Add comment · Show 1 · 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 kami1339 · Mar 28, 2019 at 12:38 PM 0
Share

Solved will work

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




Follow this Question

Answers Answers and Comments

6 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Camera don't follow sphere as was expected 0 Answers

How to activate Mesh Renderers on Trigger Collission 1 Answer

Passing animation state into attack method 0 Answers

How to make multiple game object work with the same scripts? 0 Answers

Problem making my character walk backward 0 Answers


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