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 Marks981 · May 22, 2015 at 02:39 PM · c#scripting problemscriptingbasicsenabled

How to get access to C# script in another way.

Hello smart people,i have very simple question for you.

How can i disable a C# script FROM ANOTHER script?

Yeah,yeah i know,i know there are TONS of answers and solutions out there that can make this work...

Im going to show you my script and afterwards the problem.

So here are some lines from my script :

 public HitReactionCharacter Hitreaction;
 
 Hitreaction = GameObject.FindWithTag("Avatar").GetComponent<HitReactionCharacter>(); 


This script up there is correct and it should work fine,but it does not... I got error : The type or namespace name `HitReactionCharacter' could not be found. Are you missing a using directive or an assembly reference?

I KNOW how can i fix this error,I know in order to FIND and Get ACCESS TO specific c# script (from other script),i have to put that specific script in a particular folder, like "Plugins" "Standart Assets".... So then the script will find that "specific" script which needs to enabled/disabled/changed...

But i cannot do that,because if i change the script destination,everything will be messed up for me,because my script is connected to the other scripts,and if i drag that script in that special folder,i will Lost the whole connection and everything gets messed up. I Absolutely CANNOT afford to change my script location...

So here is the real question for you Guys: How can i GET ACCESS TO C# script in different way?

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 spiceboy9994 · May 22, 2015 at 02:52 PM

What's the path of your script HitReactionCharacter. It may not be an issue with the path, but more a Namespace issue. Namespaces are logical groups for classes. Go to your HitReactionCharacter and check what namespace is specified just before the class defition. It should be something like

 namespace MyCustomNamespace.SubNamespace
 {
    public class HitReactionCharacter: MonoBehavior
    {
 
    }
 }

It could or could not have a sub namespace. The only thing that you need to do, on all the classes where you want to use that script is to include the namespace with a using Statement on the top of the class file.

 using MyCustomNamespace.SubNamespace;

By adding that using, you should not have that issue anymore.

This is how C# works in terms of classes and namespaces. Hope this works for Unity.

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 Marks981 · May 23, 2015 at 09:57 AM 0
Share

Hello spiceboy9994,i tried doing this thing with namespaces before,i didnt work,also i cant afford to do that,cause my script already have namespace which holds other scripts.

Thanks for your help?

avatar image
1

Answer by pako · May 22, 2015 at 05:55 PM

The problem seems to be with the order of compilation of scripts. This is why the problem could be solved, if you placed your script in a "Special Folder". Here's a reference if you want more info:

http://docs.unity3d.com/Manual/ScriptCompileOrderFolders.html

So, what seems to be happening is that the following instruction in your code:

 GameObject.FindWithTag("Avatar").GetComponent<HitReactionCharacter>();

...runs before HitReactionCharacter script compiles.

This is why you get the error message that "type or namespace name `HitReactionCharacter' could not be found". It's not found, because it hasn't compiled yet.

Since you want a workaround, you can use the sript "Execution Order" as follows:

  1. Select any script inside the Project tab.

  2. Click on the button "Execution Order" at the top right of the Inspector.

  3. Drag and Drop the HitReactionCharacter in the "Execution Order" (MonoManager) in the Inspector.

  4. Drag and Drop the HitReactionCharacter above the "Default Time" separator.

Comment
Add comment · Show 4 · 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 Marks981 · May 23, 2015 at 09:48 AM 0
Share

Hi Pako,very nice explanation you have there, anyway i did everything you said i should do,but still i get error massage : "type or namespace name `HitReactionCharacter' could not be found" :(

avatar image Marks981 · May 23, 2015 at 10:08 AM 0
Share

Hello People,just a quick question,is there any way to do this with VARIBALES? I tried this before,with method:

 public Component HitReactionCharacter;
     
     
     HitReactionCharacter = GameObject.FindWithTag("Avatar").GetComponent("HitReactionCharacter");  


The thing is that it WOR$$anonymous$$S,it finds the component as script. BUT,when i try to manipulate the code,like,you know disable and enable it (just what i need) : HitReactionCharacter.enabled = false;

It shows error : 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 a using directive or an assembly reference?)

I think i Understand what the error means and i dont like that fact.

This seems very simple,handy method. But it does not want to work,maybe you guys know some other methods like this one?

avatar image Marks981 · May 23, 2015 at 10:10 AM 0
Share

Thumbs up for people who help,Thanks,i very appreciate that!

avatar image pako · May 23, 2015 at 10:59 AM 0
Share

You cannot enable/disable components. You can only enable/disable the GameObject that a component is attached to, using GameObject.SetActive(true/false):

http://docs.unity3d.com/ScriptReference/GameObject.SetActive.html

This means that even if you didn't get the error using the code in your question, it wouldn't work for you, because Hitreaction.enabled = false would throw an error (similar to what you go using Component).

So, maybe you have to re-think how you want to do things.

Other than that, the change you made (and worked) has nothing to do with the variable type, but because you used a different from of GetComponent:

 A. GetComponent<HitReactionCharacter>() //original code - does not work
 B. GetComponent("HitReactionCharacter")()// changed code - works
 

Usually, you use form 'A' for better performance, but form 'B' if you are trying to access a C# script from Javascript, because it's not possible to get the type of a C# script from Javascript (hence your original error and why it worked when you changed it to form 'B'):

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

It wasn't clear to me that you were trying to access a C# script from Javascript. I thought you had 2 C# scripts.

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

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Can I make Money collecting script without attaching it to a object ? I tried but I got error 2 Answers

Dissolve shader script little proplemo, help me 0 Answers

Use vim with unity? 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