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 Scorpox · Mar 13, 2015 at 04:48 PM · unity 5variablesnamespacefirst person controllermodify

Modify a variable between namespaces

Hi everyone. My question is simple, and I suppose the answer is simple, but I haven't found out the solution yet. I'm using Unity 5.0.2 and doing some practices with the new first person controller. I would like to access other script's variables from this script, but as this controller is into a namespace, It does not find the other script, that has no namespace... If I try to access the controller from another script I just have to put the complete namespace and it works.

How could I access the variable of another script from the controller?

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

Answer by JC_SummitTech · Mar 23, 2017 at 10:28 PM

If I understand correctly, what you want is to access a variable/function form another MonoBehaviour. any member that is not static won't be accessible unless you have access to an instance of the Monobehaviour. If the other script is in the same gameObject as the CharacterController, you cna access the instance by calling YourClassName myInstance = GetComponent(); then myInstance.MyPublicProperty;

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 unity_neIDb-PA4Kmwdw · Aug 20, 2020 at 07:13 PM 0
Share

Wow that worked, this helped me create a working difficulty system for my pong game. Thanks!

avatar image
0

Answer by TD10074405 · Mar 13, 2015 at 05:05 PM

Try creating an instance of the other file i.e

 public class TestClass : MonoBehaviour {
  public ClassIWant EasyNameToFind;
 
  public void SomeFunction(){
   EasyNameToFind.TheFunctionYouWant();
    or
   EasyNameToFind.SomeVariable = TheModifiedValue;
  }
 }


That should be a simple enough way to do it, if I'm understanding you correctly. The "EasyNameToFind" class would then show up in the inspector of your class you're accessing it from, aslong as the Class You want is Serialized

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 Scorpox · Mar 15, 2015 at 03:14 AM 0
Share

I can do that just to call a function of CharacterController.cs, but I'd like to call a function of weaponScript.cs from CharacterController.cs, I don't know if you understand me...

CharacterController.cs

  namespace UnityStandardAssets.Characters.FirstPerson
  {
      public class FirstPersonController : $$anonymous$$onoBehaviour
      {
        int number = 0;
        //I wanna call here a function from the following script
        void SomeFunction(){}
      }
  }

WeaponScript.cs

  public class WeaponScript : $$anonymous$$onobehaviour{
      void DoSth()
      {
      }
  }

Although, I can call a function of CharacterController.cs from WeaponScript.cs.

I hope it's more understandable like this.

Thanks a lot.

avatar image vverma9 Scorpox · Mar 23, 2017 at 12:12 AM 0
Share

How to do the other way round? i.e. access the variables of WeaponScript inside FirstPersonController

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

25 People are following this question.

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

Related Questions

How to send simple values on local network? 1 Answer

Unity auto unassigned skinned mesh render variables on Play 1 Answer

How to change UI image with a name to a different image with a name 2 Answers

Make an animation position equivalent to a variable 1 Answer

How can i save a variable for only one scene? 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