• 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
Question by Marcus_is_beast · Feb 24, 2019 at 04:36 AM · unity 5function call

Can't Call Function From Other Script

Hello, I am fairly new to Unity, i have looked at other posts on how to call functions from other scripts but they don't seem to work. I am trying to call ReturnToStorage() from Text Control, I was wondering if anyone could help me call the functions. Thanks

ReturnToStorage() function details

  public void ReturnToStorage()
     {
         Debug.Log("ReturnToStorage");
         //currentTile.PlayerStone = null;
         //currentTile = null;
 
         this.isAnimating=true;
         theStateManager.AnimationsPlaying++;
 
         moveQueue = null;
 
         // Save our current position
         Vector3 savePosition = this.transform.position;
 
         MyStoneStorage.AddStoneToStorage( this.gameObject );
 
         // Set our new position to the animation target
         SetNewTargetPosition(this.transform.position);
 
         // Restore our saved position
         this.transform.position = savePosition;
     }

TextControl details

 if (choiceSelected == "y")
         {
             choiceSelected = "n";
             if (correctAnswer[randQuestion]==selectedAnswer)//checks slected answer is correct
             {
                 resultObj.GetComponent<TMPro.TextMeshProUGUI>().text = "Correct Answer!!" ;//correct answer
             }
             else
             {
                 BackToMenu.resetCorrectAnswer = "n";
                 //Add Function
                 resultObj.GetComponent<TMPro.TextMeshProUGUI>().text = "Incorrect Answer!!";//wrong answer
 
                 if (correctAnswer[randQuestion] == "1")
                 {
                     correctAnswerObj.GetComponent<Transform>().position = new Vector3(140, 290, 0);
                 }
                 if (correctAnswer[randQuestion] == "2")
                 {
                     correctAnswerObj.GetComponent<Transform>().position = new Vector3(630, 290, 0);
                 }
                 if (correctAnswer[randQuestion] == "3")
                 {
                     correctAnswerObj.GetComponent<Transform>().position = new Vector3(630, 170, 0);
                 }
                 if (correctAnswer[randQuestion] == "4")
                 {
                     correctAnswerObj.GetComponent<Transform>().position = new Vector3(140, 170, 0);
                 }
             }
         }

Comment

People who like this

0 Show 2
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 28mitu · Feb 24, 2019 at 05:12 AM 0
Share

@Marcus_is_beast from your code snippet, I am not able to find the method name that you are calling from Text control details script. However, you can refer below links maybe it will be helpful for you.

https://answers.unity.com/questions/7555/how-do-i-call-a-function-in-another-gameobjects-sc.html

https://forum.unity.com/threads/calling-function-from-other-scripts-c.57072/

avatar image Marcus_is_beast 28mitu · Feb 24, 2019 at 06:10 AM 0
Share

I did try this, but i get a error. NullReferenceException: Object reference not set to an instance of an object TextControl.Update () (at Assets/Scripts/QuestionsScripts/TextControl.cs:43)

here is apart of my code

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class TextControl : MonoBehaviour
 {
     List<string> questions = new List<string>() {"1","2","3","4","5","6","7","8","9","10","11"}; //Questions
     List<string> correctAnswer = new List<string>() {"4", "1", "3", "4", "1", "2", "3", "4", "1", "2", "3" }; //Correct Awnser 
     public Transform resultObj;
     public static string selectedAnswer;
     public static string choiceSelected = "n";
     public static int randQuestion=-1;
     public Transform correctAnswerObj;
     public PlayerStone wrongAnswer;
     // Start is called before the first frame update
     void Start()
     {
        // GetComponent<TMPro.TextMeshProUGUI>().text = questions[0];
     }
 
     // Update is called once per frame
     void Update()
     {
         if (randQuestion == -1)
         {
             randQuestion = Random.Range(0, 10);
         }
         if (randQuestion > -1)
         {
             GetComponent<TMPro.TextMeshProUGUI>().text = questions[randQuestion];
         }
         //Debug.Log(questions[randQuestion]);
         if (choiceSelected == "y")
         {
             choiceSelected = "n";
             if (correctAnswer[randQuestion]==selectedAnswer)//checks slected answer is correct
             {
                 resultObj.GetComponent<TMPro.TextMeshProUGUI>().text = "Correct Answer!!" ;//correct answer
             }
             else
             {
                 BackToMenu.resetCorrectAnswer = "n";
                 wrongAnswer.ReturnToStorage();
                 resultObj.GetComponent<TMPro.TextMeshProUGUI>().text = "Incorrect Answer!!";//wrong answer
 
                 if (correctAnswer[randQuestion] == "1")
                 {
                     correctAnswerObj.GetComponent<Transform>().position = new Vector3(140, 290, 0);
                 }
                 if (correctAnswer[randQuestion] == "2")
                 {
                     correctAnswerObj.GetComponent<Transform>().position = new Vector3(630, 290, 0);
                 }
                 if (correctAnswer[randQuestion] == "3")
                 {
                     correctAnswerObj.GetComponent<Transform>().position = new Vector3(630, 170, 0);
                 }
                 if (correctAnswer[randQuestion] == "4")
                 {
                     correctAnswerObj.GetComponent<Transform>().position = new Vector3(140, 170, 0);
                 }
             }
         }
     }
 }
 




1 Reply

· Add your reply
  • Sort: 
avatar image
Best Answer

Answer by 28mitu · Feb 24, 2019 at 07:23 AM

@Marcus_is_beast you need to initialize game object under "start method".

  void Start()
      {
        wrongAnswer= FindObjectOfType<PlayerStone>();
      }

   
Comment

People who like this

0 Show 0 · 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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

192 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 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

how to check button click in between running function ? 1 Answer

I can't use Unity! 2 Answers

simple pop up ads 0 Answers

Unity 5 3D Space Movement Issues 0 Answers

Check collision from another object's script 4 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