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.
@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.
@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
@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!
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.
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");
}
}
}
Just make
void YouWin(){}
to
public void YouWin(){}
Your answer
Follow this Question
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