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 kmccmk9 · Dec 20, 2011 at 06:47 AM · scriptingbasicsreferenceaccess

Access functions in another script

Hello, can someone please provide a step by step tutorial on c# code on how to access functions in another script I have? I have looked at many posts and code resources but they just give me errors.

Comment
Add comment · Show 2
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 DaveA · Dec 20, 2011 at 06:50 AM 0
Share

Assu$$anonymous$$g you read http://unity3d.com/support/documentation/ScriptReference/index.Accessing_Other_Game_Objects.html, then you should post your scripts here so we can see what you're doing wrong

avatar image kmccmk9 · Dec 20, 2011 at 06:57 AM 0
Share

Ok, well ok using this code I can access the other script: void Start () { GameObject femaleNude = GameObject.Find("femalenude"); FemaleNude femalenudeScript = femaleNude.GetComponent(); femalenudeScript.ChangeEnabled(false); }

But, when using the code later, in another function I get femalenudeScript is unknown error: femalenudeScript.ChangeEnabled(true);

4 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by jahroy · Dec 20, 2011 at 08:41 AM

Here is BY FAR the easiest way to do this:

Here is how to access a script named TheOtherScript that is defined in a file named TheOtherScript.cs from another scrip named TheAccessor.

TheAccessor.cs:

public TheOtherScript scriptReference;

void Update () { Debug.Log(scriptReference.getCodeWord()); }

void Start () { if ( scriptReference == null ) { Debug.Log("script reference is null: please assign in Inspector"); } }

TheOtherScript.cs:

public class OtherScript : MonoBehaviour

public string secretCodeWord = "Super Funkily Dunkily";

public string getCodeWord () { return secretCodeWord; }

Now all you have to do is follow these steps to get it to work:

  • attach a TheOtherScript to an object
  • attach a TheAccessor script to a different object
  • select the object with the TheAccessor script attached to it (left-click)
  • look in the Inspector
  • observe a slot named "The Other Script"
  • drag the item with the TheOtherScript attached to it onto the slot
  • enjoy

You should see the code word printed in the debug console every frame.

By the way... I don't use C#, so I'm sure there are some syntax errors in there.

This is just meant to be an example.

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
avatar image
0

Answer by RRMessiah · Dec 20, 2011 at 06:02 PM

Correct me if I'm wrong, but the EASIEST way to access another script's functions is if that script is of a static class with object inheritance.

Make this script and store it anywhere in your project. You don't need to attach it to a gameobject. Just leave it in a folder.

     using UnityEngine;
     using System.Collections;
     
     public static class StaticClasses : object {
     
         public static void DebugFunction()
         {
         Debug.Log("You called the function.");    
         }
 
 }

Then, in any script you have in the entire project, you can just call that function by just doing StaticClasses.DebugFunction();

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 androiddevelo · Aug 27, 2014 at 07:56 AM 0
Share

i tried that one, but why is the system tells me that

The name 'StaticClasses'does not exist in the current context

avatar image
0

Answer by kmccmk9 · Dec 20, 2011 at 07:15 AM

Ok, well ok using this code I can access the other script:

 void Start () { 
 GameObject femaleNude = GameObject.Find("femalenude"); 
 FemaleNude femalenudeScript = femaleNude.GetComponent(); femalenudeScript.ChangeEnabled(false); 
 }

But, when using the code later, in another function I get femalenudeScript is unknown error: femalenudeScript.ChangeEnabled(true);

Comment
Add comment · Show 3 · 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 DaveA · Dec 20, 2011 at 08:28 AM 0
Share

You have to GetComponent in each function, or better yet, set a 'global' variable in Start and then use that variable in the other functions.

avatar image kmccmk9 · Dec 20, 2011 at 04:50 PM 0
Share

Ok what is the best way to define a global variable. I tried putting public in front but that doesn't work, I tried putting public before the start function, and that also didn't work. When I do actual c# program$$anonymous$$g, I just define everything before the main function or any function to make it global.

avatar image jahroy · Dec 20, 2011 at 05:25 PM 0
Share

I suggest you forget about GetComponent and do it the easy way.

See the other answer...

avatar image
0

Answer by kmccmk9 · Dec 21, 2011 at 09:38 AM

Thank you for all the replies. Ok using this code I was able to access the script. However, when trying to access another script attached to another model, it does not work. It does not return errors but my Male Character still shows when the scene starts.

GameObject femaleNude; FemaleNude femalenudeScript; GameObject maleNude; MaleNude malenudeScript;

 // Use this for initialization
 void Start () {
     femaleNude = GameObject.Find("femalenude");
     femalenudeScript = femaleNude.GetComponent<FemaleNude>();
     femalenudeScript.ChangeEnabled(false);
     maleNude = GameObject.Find("malenude");
     malenudeScript = maleNude.GetComponent<MaleNude>();
     malenudeScript.ChangeEnabled(false);
 }

I did find out the reason it isn't working. The script has to be applied to all children and not just the parent. How can I do this if I have a large amount of children?

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

7 People are following this question.

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

Related Questions

How do I call a function in another gameObject's script? 5 Answers

Methods of Accessing scripts - performance & neatness 1 Answer

Is constantly referencing or defining once then changing better 1 Answer

Having an object reference itself 2 Answers

Multiple objects with the same script having trouble accessing another script/object 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