Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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
1
Question by Martacus · Dec 20, 2015 at 12:40 AM · c#unity 5objectsfunctions

Call function of a MonoBehaviour class in another class

So i am trying to call a function in this class: using UnityEngine; using UnityEngine.UI; using System.Collections;

 public class AchievementDisplay : MonoBehaviour {
 
     Text text;
 
     void Start()
     {
         gameObject.SetActive(true);
         text = GetComponent<Text>();
        
     }
     
     
     void Update () {
     
     }
 
    public void achievementActivate(string achText) {
         Debug.Log("PLEB");
         text.text = achText;
         moveUp();
 
     }
 
     void moveUp() {
         var t = 0;
         while (t < 100) {
             transform.Translate(Vector3.up * Time.deltaTime);
         }
         Invoke("moveDown", 5);
     }
 
     void moveDown() {
         var t = 0;
         while (t < 100)
         {
             transform.Translate(Vector3.down * Time.deltaTime);
         }
     }
 }
 

via this class: using UnityEngine; using System.Collections; using System.Collections.Generic; using System;

 public class AchievementManager : MonoBehaviour {
 
     // This class is used to check for achievements and give. I have no idea how to do this. Help would be cool.
 
     List<Achievement> ach = new List<Achievement>();
     List<Achievement> activatedAch = new List<Achievement>();
 
     AchievementDisplay achievedis = new AchievementDisplay();
 
 
     void Start () {
         ach.Add(new Achievement(2, "clicks", "poopie"));
     }
 
     void Update () {
         foreach(Achievement achie in ach){
             if (achie.theSomething == "clicks"){
                 if(achie.amountOfSomething == Data.clicks){
                     achie.activated = true;
                 }
             }
 
             if (achie.activated) {
                 achie.activated = false;
                 activatedAch.Add(achie);
                 achievedis.achievementActivate(achie.text);
                 ach.Remove(achie);
             }
         }
     }
 }

But i am getting this error: You are trying to create a MonoBehaviour using the 'new' keyword. This is not allowed. MonoBehaviours can only be added using AddComponent(). Alternatively, your script can inherit from ScriptableObject or no base class at all UnityEngine.MonoBehaviour:.ctor() AchievementDisplay:.ctor() AchievementManager:.ctor()

Is there a good way to do this?

Comment
Add comment · 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 gjf · Dec 18, 2015 at 03:52 PM 0
Share

as the error states, you're trying to new a $$anonymous$$onoBehaviour where you should be using AddComponent().

if you want to get a reference to the AchievementDisplay on another game object then use GetComponent() after finding the object in question (either by storing a reference to its game object or using Find()

avatar image hitarthdoc1994 · Jan 30, 2016 at 05:27 AM 0
Share

I would recommend you to see the following link, I think it is a suitable answer for you.

http://answers.unity3d.com/questions/451004/how-to-call-a-function-from-one-class-to-another-c.html

0 Replies

· Add your reply
  • Sort: 

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Array.Sort() - JS only???! 0 Answers

Calling Function from another script not working 1 Answer

Function not running everytime 0 Answers

How can I change a function from another script? 1 Answer

Difference between C# Invoke and Unity Invoke 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