• 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 /
  • Help Room /
avatar image
0
Question by Profsnail · Nov 17, 2020 at 07:10 PM · functionscriptingproblemobject referencescript referencing

Alternative to FindObjectOfType and calling function from another script

Hi, I'm relatively new to unity and I'm trying to call a function from another script to update the score of the game whenever i destroy a gameobject (target), but I can only do it with FindObjectOfType, but I have heard that it is very slow? I'm just wondering if there are better ways to call the AddToScore function from my "Score" script while in my "Target" script, and also if there is a better alternative to this and it would be great if any kind soul could help me with speeding up my code!

My game involves spawning a target, and on click it will disappear and a new target will spawn, and everytime you click on the target your score increases by 1.

Target script:

 using UnityEngine;
 
 public class Target : MonoBehaviour
 {
     [SerializeField] private TargetLoader tl; 
     [SerializeField] private int scoreValue = 1;
     
     void OnMouseDown()
     {
         Destroy(gameObject);
         FindObjectOfType<Score>().AddToScore(scoreValue);
         tl.SpawnCircle();
     }
 }

Score script:

 using TMPro;
 using UnityEngine;
 
 public class Score : MonoBehaviour
 {
     [SerializeField] int initialScore = 0;
     public TextMeshProUGUI score;
 
     private void Start()
     {
         initialScore = 0;
         score.text = initialScore.ToString();
     }
 
     public void AddToScore(int scoreValue)
     {
         initialScore += scoreValue;
         score.text = initialScore.ToString();
     }
 }

alt text

Thank you so much in advance!

unity-help.jpg (161.8 kB)
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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by xxmariofer · Nov 18, 2020 at 10:32 AM

There are alternatives but they are also slow like finding the gameobject and getting componentes, what you have to do is catch the score in a variable in the start so you only call the FindObjectOfType once (or simply make it serializefield and drag and drop frmo inspector)

  using UnityEngine;
  
  public class Target : MonoBehaviour
  {
      [SerializeField] private TargetLoader tl; 
      [SerializeField] private int scoreValue = 1;
      private Score score;
 
     void Start()
     {
        score = FindObjectOfType<Score>();
     }
      
      void OnMouseDown()
      {
          Destroy(gameObject);
          score.AddToScore(scoreValue);
          tl.SpawnCircle();
      }
  }

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 Profsnail · Nov 18, 2020 at 02:24 PM 0
Share

Thank you so much! Yes I've heard of the GetComponent way, how do I go about doing that?

avatar image xxmariofer Profsnail · Nov 18, 2020 at 02:41 PM 0
Share

The GetComponent is the same as FindObjectOfType but you search in one specific gameobject (really usefull when you have multiples instances of the class in the scene)

 gameObject.GetComponent<Score>();

but you need to find the gameObject reference first, using FindObjectWithTag or GameObject.Find for example or by drag and drop in the inspector

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

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

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

The type or namespace function could not be found 1 Answer

Simple Question 2 Answers

Load Scene Unity by Application.LoadLevelAsync 0 Answers

Problem about finding script name 1 Answer

Calling a function from another script 0 Answers

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges