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 YoungDeveloper · Aug 15, 2013 at 02:38 PM · accesspublicaccessing

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.

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

3 Replies

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

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();
     }
 }
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
3

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
     }
 }
Comment
Add comment · Show 7 · 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 fafase · Aug 15, 2013 at 03:47 PM 1
Share

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.

avatar image YoungDeveloper · Aug 15, 2013 at 03:52 PM 0
Share

Agree, but the CallFunction in scripts are different, only the names are the same.

avatar image Muuskii · Aug 15, 2013 at 03:56 PM 1
Share

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.

avatar image YoungDeveloper · Aug 15, 2013 at 04:00 PM 0
Share

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.

avatar image YoungDeveloper · Aug 15, 2013 at 04:27 PM 0
Share

@$$anonymous$$uuskii @fafase

Thank you very much, worked like a charm.

Show more comments
avatar image
1

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.

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

18 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

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

How do I access information (eg, variables, functions, properties) on other objects from inside a script? 2 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