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
0
Question by inediblered · Nov 16, 2013 at 12:11 PM · c#functionreferencecall

How to reference another script and call a function in C# ?

So in a script script1 I have:

    public void Follow(GameObject followTarget, bool snap = false)
     {
         Follow(followTarget.transform);
     }

How can I reference this in script2 and assign a GameObject to it?

I found tutorials and examples online but I'm really struggling

Comment
Add comment · Show 1
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 meat5000 ♦ · Nov 16, 2013 at 12:12 PM 0
Share

Use GetComponent :)

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by Hoeloe · Nov 16, 2013 at 12:15 PM

You need an instance of the script that you can reach from script2. You can do this by either having a field of type script1 that you set from the inspector (`public script1 script;`), or you can use the GetComponent method to grab it automatically.

From there, it's a simple case of writing: script.Follow(...) and filling in the arguments as necessary.

As an aside, I recommend using CamelCasing for class names (e.g. MyScript1 instead of myscript1). It helps to differentiate them from variables.

Comment
Add comment · Show 8 · 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 meat5000 ♦ · Nov 16, 2013 at 12:19 PM 0
Share

Instancing uses more memory, GetComponent uses more CPU time. So it's a trade off (in large projects)

avatar image Hoeloe · Nov 16, 2013 at 12:31 PM 0
Share

Of course, but if you don't have an instance, it is not possible to call a non-static method. If your instance already exists, then you're just paying for another pointer, which is a $$anonymous$$imal use of memory.

avatar image inediblered · Nov 16, 2013 at 12:35 PM 0
Share

could you give me a written example of this please ?

avatar image Hoeloe · Nov 16, 2013 at 12:41 PM 0
Share

BadAssGames has written one in the other answer. If you add the public modifier to both of the variable declarations, and remove the line defining script1, then you can declare those variables in the inspector.

avatar image inediblered · Nov 16, 2013 at 01:19 PM 0
Share

I tried it replacing the example I gave with the actual script name and the object name in my scene (which is tagged as player): RtsCamera RtsCamera; GameObject Player_01;

 void Start()
 {
     RtsCamera = FindWithTag("Player").GetComponent<RtsCamera>();
     
     RtsCamera.Follow(RtsCamera, false);
     
 }

and unity say that a namespace can only contain types and namespaces declarations 3 times and 'global'Assets/Scripts/Camera/RtsCamera/Scripts/RtsCamera.cs(16,14): error CS0101: The namespace global::' already contains a definition for RtsCamera'

what Am I doing wrong ?

Show more comments
avatar image
0

Answer by BadAssGames · Nov 16, 2013 at 12:18 PM

 Script1 script1;
 GameObject someGameObject;
 
 
 void Start()
 {
     script1 = FindWithTag("SomeTaggedGameObject").GetComponent<Script1>();
 
     script1.Follow(someGameObject, false);
 
 }
 
Comment
Add comment · 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

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

20 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

Related Questions

How can I call a method from another script? 3 Answers

How to call a function from another script without referencing it? 1 Answer

call function with same name + number 1 Answer

Call a Function from another Script 1 Answer

Refin' another script in relation to speed, and not tacking from using the Update? 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