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 /
  • Help Room /
avatar image
1
Question by beyluta · Mar 13, 2016 at 11:15 AM · c#functioncallvoid

How to call functions from another script c# (Unity5)

I want to call a function from script A to script B I tried watching a bunch of youtube videos and unity answers posts but none of them worked.

Comment
Add comment · Show 3
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 Salmjak · Mar 13, 2016 at 12:25 PM 0
Share

@beyluta I noticed that you deleted your comment so my reply to that comment also got deleted :P If my first comment helped you, please convert it to an answer and mark it as correct :)

I might add a short version of the comment that got deleted: Your error can happen for many reason, but probably these two are the source for your particular error:

1) The GameObject you are trying to access is never instantiated, this could happen if you use a prefab as the gameobject. A prefab should never be treated as its own gameobject, it's more of a template. So only drag gameobjects from your hierarchy to your GameObject-variable slot (or instantiate a new gameobject at runtime based on a prefab).

2) The script isn't attached to the gameobject.

avatar image beyluta · Mar 14, 2016 at 04:23 PM 0
Share

@Salmjak Sorry but for some strange reason I am unable to convert it as an answer I also wasn't able to reply earlier today but now it seem to be working again, but I'll do it as soon as I figure out why isn't it working

avatar image Salmjak beyluta · Mar 14, 2016 at 04:38 PM 0
Share

@beyluta I've seen someone convert my comment into an answer before, but I noticed after posting this that I dont have that option myself on my own questions :S I wonder what that's all about.

I converted my comment into an answer anyhow!

1 Reply

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

Answer by Salmjak · Mar 13, 2016 at 11:22 AM

@beyluta From what I can understand of your question is that you want to call a function on instance X of script A from instance Y of script B. First of all you need to have a reference to the instance. You can get this by Script A myInstance = myGameObject.GetComponent<Script A>(); where myGameObject is the gameobject on which the instance you want to use is (this only works for scripts that inherit MonoBehaviour!).

If both scripts are attached to the same gameobject you can just use gameObject.GetComponent<Script A>();.

After that it's quite simple: myInstance.myFunction();

It should be noted that the function has to be a public function!

It would really help if you actually post your code and what you have actually tried doing.

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 anjum8 · Feb 03, 2020 at 03:28 PM 0
Share

Hello sir @Salmjak Your reply will be heartidly appreciated Please help me senior members I am new to Unity and getting following error

""Assets/Scripts/Conditional.cs(14,43): error CS0122: `Functions2.YouWin()' is inaccessible due to its protection level""

I am trying to use custom function of another script file named Functions2.cs in my current script file named Conditional.cs by living in same project.

Here is the code of my Functions2.cs file

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class Functions2 : $$anonymous$$onoBehaviour
 {
     public int Health;
 
     void  Update(){
     
         if (Health <= 0) {
         
             Death ();
         }
             
         if (Health >= 50) {
         YouWin();
 
     }
     
     }
         
     void Death(){
     
         Debug.Log ("player died");
     }
 
     void YouWin(){
     
     Debug.Log ("you win");
     
     }
 
 
 
 }

Here is the code of file Conditional.cs in which I am calling custom function of above file

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class Conditional : $$anonymous$$onoBehaviour
 {
 public int score;
 public bool CheckPoint;
     public Functions2 otherScipt;
    void Update(){
     
         if (score == 80 && CheckPoint ) {
         
             otherScipt.GetComponent<Functions2> ().YouWin();
 
         }else if (score == 70 && !CheckPoint) {
 
         Debug.Log ("score is 70");
 
         } else {
         Debug.Log ("nothing");
     }
 }
 }

avatar image joker_yash96 anjum8 · Feb 04, 2020 at 05:27 AM 0
Share

Just make

 void YouWin(){}
 

to

 public void YouWin(){}

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

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

Related Questions

What go inside the brackets in voids? 1 Answer

Not finding a class in inspector (Easy) 0 Answers

How can i enable and disable a void function on the update function? 1 Answer

Why cant I access a PUBLIC function (Cs, VS2015 and 2D)? Help? 1 Answer

Setting up a function for sorting specific triggers entered based on a height integer. 0 Answers


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