Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 /
  • 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.

Follow this Question

Answers Answers and Comments

216 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 avatar image

Related Questions

The type or namespace function could not be found 1 Answer

call any method 1 Answer

How to pause execution until function terminates 1 Answer

Can somebody tell me which functions I would need to use to make this pseudocode work? 1 Answer

Function only rotates one update-cycle 0 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