- Home /
Accessing not yet publicly added scirpt
Hi, here is a simple code which accesses other scripts function. But the script has not been added yet so I believe that is why it won't compile, any suggestion ? Or maybe I'm doing something wrong.
Here's the code:
using UnityEngine;
using System.Collections;
public class Manager : MonoBehaviour {
public MonoBehaviour Script;
public void Function(){
Script.CallFunction();
}
}
Error:
error CS1061: Type UnityEngine.MonoBehaviour' does not contain a definition for
CallFunction' and no extension method CallFunction' of type
UnityEngine.MonoBehaviour' could be found (are you missing a using directive or an assembly reference?)
Thank you.
Answer by fafase · Aug 15, 2013 at 02:51 PM
Your problem comes from the type of the Script variable. You made it MonoBehaviour I guess with the idea to add any type of script.
Problem is that CallFunction does not exist in the MonoBehaviour class hence the error.
What you could do is used your own class or better an interface containing the method. Then any class that may be used in your manager implements the interface and you are good to go.
Problem, interface cannot implement MonoBehaviour and won't be turned into component so you may go with abstract class.
public abstract class IManager{
public abstract void CallFunction();
}
public class MyClass:IManager{
public override void CallFunction(){
// Do something
}
}
public class Manager : MonoBehaviour {
public IManager Script;
public void Function(){
Script.CallFunction();
}
}
Answer by Muuskii · Aug 15, 2013 at 02:47 PM
Your field is of type MonoBehavior, which doesn't define your function. Change the type to your script name.
If you want to attach different types to your script you're going to need a superclass that defines the method you're trying to access. It will have to inherit MonoBehavior and all the scripts you want to attach will have to implement it.
Something like:
abstract public class SuperClass : MonoBehavior
{
abstract void CallFunction();
}
public class YourScript : SuperClass
{
override void CallFunction()
{
// Concrete implementation here
}
}
That is not so good thinking. You should go for the mentioned version which makes it more efficient and faster.
You could also go with interface that allows multiple inheritance but then you need to go with FindObjectOfType ins$$anonymous$$d.
Agree, but the CallFunction in scripts are different, only the names are the same.
This sort of thing is exactly what inheritance in type safe languages such as C# was designed for. Why don't you test out the solution Fafase and I are suggesting? You may be pleasantly surprised.
Thanks, I just never had any experience with abstract classes and ovverride, and I don't really know and understand (for now) how they work. But i will try and post back.
@$$anonymous$$uuskii @fafase
Thank you very much, worked like a charm.
Answer by programmrzinc · Aug 15, 2013 at 02:47 PM
To access another function from a script, you can use the SendMessage function. All you have to do is attach the script to a gameobject.
What i Think will solve you problem is to add the script to a gameobject, and access it using the gameobject,
GameObject TestOBJ has script TestSPT attached,which inturn has function CallMe.
public GameObject TestOBJ;
void Start(){
TestOBJ.TestSPT.CallMe("test");
}
Both scripts have to be C though.
Your answer
Follow this Question
Related Questions
Is it possible to change a variable, into a script not assigned to any game object? 3 Answers
Can I make variables visible to other scripts without making them visible in the Inspector? 1 Answer
Problem: Accessing Other Script Variables 3 Answers
How do you access a script if you don't know it's name? 5 Answers