Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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
1
Question by Kissanaattori · Dec 17, 2014 at 10:11 PM · c#cameratoggle

Toggle between scripts with a script?

Hello there. My camera for a third person shooter has two scripts: when you are aiming and when you are not. My question is, is it possible to make a script that toggles between these two scripts with a press of a button? I use c# as scripting language. Thanks in advance.

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

Answer by weitay · Dec 18, 2014 at 07:39 AM

use the GetComponent() function, it can look for scripts as well.

So for example

 public TestScript scriptReference;
 
 scriptReference = GetComponent<TestScript>();


http://docs.unity3d.com/ScriptReference/Component.GetComponent.html

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 Kissanaattori · Dec 18, 2014 at 05:00 PM 0
Share

Thanks, but how do I enable/disable the script? There is no ScriptReference.enable in c# as far as I know.

avatar image MrSoad · Dec 18, 2014 at 05:09 PM 0
Share

Try :

 scriptReference.enabled = true;
 
 scriptReference.enabled = false;

avatar image
1

Answer by Graham-Dunnett · Dec 17, 2014 at 10:11 PM

Yes, it is possible.

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 MrSoad · Dec 17, 2014 at 10:16 PM 0
Share

To elaborate slightly on Grahams answer... You need to find both of the scripts in question in your Toggle Script(Find the objects they belong to and the get the script component for each one).

Now in your Toggle script, when the keypress event happens, check if one script is enabled and if so disable that script and enable the other. Reverse this if not...

avatar image Kissanaattori · Dec 18, 2014 at 06:57 AM 0
Share

But how do I find the script component? Best way to do this would likely be to assign the script to a variable, but what is the argument to assign it? Thanks in advance.

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Distribute terrain in zones 3 Answers

Adding a listener to Toggle.onValueChanged via Script 3 Answers

[C#]Toggle Run on/off 2 Answers

No Glitch Please ... 1 Answer

Camera Script - RtS style, rotation issues 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