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 Gianluca88 · Aug 19, 2014 at 03:30 PM · c#functionmethodcall

How can I call a method from another script?

Hi all, I read lots of similar question on unity community but I can't resolve my problem. I have 2 scripts. LeftShoulder.cs that has a method: prova(). From MainGUI.cs I want call the method prova(). But Unity return me this error: object reference not set to an instance of an object .

This is my code.

     using UnityEngine;
     using System.Collections;
     
     public class LeftShoulder : MonoBehaviour {
     
         public Transform from;
         public Transform to;
         public float speed = 100.0F;
         public Component[] allcomponent;
     
         // Use this for initialization
         void Start () {
             allcomponent=gameObject.GetComponents(typeof(Component));
     //        for(int i=0;i<allcomponent.Length;i++){
     //            Debug.Log(allcomponent[i]);
     //        }
     
         }
         
         // Update is called once per frame
         void Update () {
     //        transform.Rotate (1f, 0f, 0f);
     
     //         ruota ();
     
     //        transform.rotation = Quaternion.Slerp(from.rotation, to.rotation, Time.time * speed);
     
         }
         public void ruota(int control){
             if (control==1) {
                             transform.Rotate (Vector3.right * Time.deltaTime * 10);
                     }
             }
     }


and MainGUI.cs

 using UnityEngine;
 using System.Collections;
 
 public class MainGUI : MonoBehaviour {
     public Component spallasx;
     public LeftShoulder spalla;
     // Use this for initialization
     void Start () {
     
     }
     
     // Update is called once per frame
     void Update () {
     
     }
 
     void OnGUI () {
         GUI.BeginGroup(new Rect(0,0,120,120));
         GUI.Box (new Rect (0,0,120,120), "Menu");
         int a = 0;
         if (GUI.Button (new Rect (10, 20, 100, 30), "primo bottone")) {
 //            spallasx = gameObject.GetComponent("LeftShoulder");
 //            Debug.Log("a");
             a = 1;
             spalla.ruota(a);
 
                 }
         GUI.EndGroup ();
     
         }
 }
 





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 smoggach · Aug 19, 2014 at 03:50 PM

You have to drag the gameobject with the LeftShoulder script onto the MainGUI's spalla property in the inspector.

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 Gianluca88 · Aug 19, 2014 at 04:01 PM 0
Share

I try it, but I have the same error.

avatar image Gianluca88 · Aug 19, 2014 at 04:02 PM 0
Share

Now go, thank you!

avatar image
3

Answer by Pecek · Aug 19, 2014 at 03:49 PM

Use the search box please, this is probably the most common question here.

http://unity3d.com/search?refinement=answers&gq=access%20another%20script

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
Wiki

Answer by DVFrance · Aug 19, 2014 at 04:36 PM

try something like this :

 private GuiGame guiGameScript;
 private GameObject guiGameObject;
 
 void Awake()
     {
         guiGameObject = GameObject.Find ("GuiGameObject");
         guiGameScript = guiGameObject.GetComponent<GuiGame>();
 }

 void Crash()
 {
 guiGameScript.CrashCounter();
 }



here GuiGame is the name of the script I like to call. Once your script is found you could execute any of the public void you have in :

 public class GuiGame : MonoBehaviour {  
   public void CrashCounter()
         {
     do your things !!!
     }
 }


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

25 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

Related Questions

How to reference another script and call a function in C# ? 2 Answers

Integer Getter Method always returns zero 3 Answers

Call a Function from another Script 1 Answer

List.add() not working when function called by 2nd script 1 Answer

Why can't I call function from another script? 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