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 /
avatar image
2
Question by Kacer · Aug 18, 2011 at 09:26 AM · c#returngetmethodsnonstatic

Accessing a Nonstatic method from another script

So, for the first time in several weeks my google-fu has failed me, and here i am.

What i want to do is to access a method i've got in script A from script B.

script A example:

 void printStuff(string stuff)
 {
      print(stuff)
 }

I want to give printStuff a value which is made in script b, but i dont want to make it static, my scripting genes tell me to use get/return, but i havnt been able to find anything that explains me how it works.

So can anyone here?, i know the question is not directly related to Unity, probably more C#.

Thanks in Advance

  • Kacer

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

Answer by CHPedersen · Aug 18, 2011 at 10:35 AM

When something isn't static, you need to access a particular instance of the script in question. This is typically done in one script, by calling GetComponent<> on the GameObject, to which the other script is attached. Here's a small example (C#). A and B are attached to two different GameObjects:

public class A : MonoBehaviour { void Start() { B instanceOfB = GameObject.Find("Name of GameObject to which B is attached").GetComponent<B>(); instanceOfB.printStuff("this is b's method"); } }

public class B : MonoBehaviour { public void printStuff(string blah) { print(blah); } }

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 Kacer · Aug 18, 2011 at 11:19 AM 0
Share

you're a lifesaver, thanks

avatar image CHPedersen · Aug 18, 2011 at 11:23 AM 0
Share

You're quite welcome. :)

avatar image
1

Answer by Rustam-Ganeyev · Aug 18, 2011 at 10:37 AM

There are two ways: 1) make your script singleton - see in wiki community. But this way is not always useful.

2) every script is attached to gameobject. Use GameObject GetComponentto get it.

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

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

Use of unassigned variable? Script from script reference 1 Answer

how to set a value in one script and call it in another? (c#) 1 Answer

Turning pieces of code into a method 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