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 /
avatar image
0
Question by Furiion · Mar 22, 2017 at 09:42 AM · errorerror messagecs0246

Trouble Calling a Function from Another Script

Hey all, I'm getting the following;

error CS0246: The type or namespace name `Pause' could not be found. Are you missing an assembly reference?

In reference to this line in one of my scripts;

 PauseGame pausegame = GetComponent<Pause>();

I'm attempting to grab the following function from another of my scripts named PauseGame.cs

 public void Pause()

I've checked a few forums and some similar questions which resolved some other issues with this piece of code I had, but now I can't seem to figure out what to do. New to Unity so I don't understand 100% of the concepts but I can't see what I'm doing wrong. Any help is much appreciated.

Comment
Add comment · Show 1
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 Hellium · Mar 22, 2017 at 01:30 PM -1
Share

Re$$anonymous$$der @Furiion, @ShadyProductions :

FAQ :

Some reasons for getting a post rejected:

  • Posting about a specific compiling error or NullReferenceException: there is a myriad of these questions with answers already, have a look at those posts to get hints on what could possibly be your issue. Also, take a look at the Unity's support website, many errors and how to solve them are described here

2 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by ShadyProductions · Mar 22, 2017 at 01:48 PM

 private PauseGame pg;
 
 void Start() {
 pg = GameObject.FindWithTag("GameManager").GetComponent<PauseGame>();
  }

 pg.Pause();

This should work perfectly then. It's a good idea to do this in the start function, and keep it as a reference, so you don't have to call it every single time (its not good performance)

Comment
Add comment · Show 1 · 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 Furiion · Mar 22, 2017 at 01:54 PM 0
Share

Legend, thank you!

avatar image
1

Answer by ShadyProductions · Mar 22, 2017 at 09:43 AM

 GetComponent<PauseGame>().Pause();

Your component will be the name of your script and from your script you can access your method.

Comment
Add comment · Show 16 · 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 Furiion · Mar 22, 2017 at 12:46 PM -1
Share

Cheers! New code is now;

 PauseGame pausegame = GetComponent<PauseGame>().Pause();


Which gives me a new error... error CS0029: Cannot implicitly convert type void' to PauseGame' Interesting one. Let me know what you think, but thank you for the earlier issue!

avatar image ShadyProductions Furiion · Mar 22, 2017 at 12:49 PM 0
Share
 GetComponent<PauseGame>().Pause();

This is enough because the method returns void, and you cannot store void inside a variable of PauseGame.

You could do this ins$$anonymous$$d:

 PauseGame pausegame = GetComponent<PauseGame>();
 pausegame.Pause();
avatar image Furiion ShadyProductions · Mar 22, 2017 at 12:56 PM 0
Share

Using your suggested code I get no errors but the intended function doesn't occur. (Triggering the pause menu function upon death). However no errors come up which is a nice change. However your other suggestion returns the following error; error CS0305: Using the generic method UnityEngine.Component.GetComponent<T>()' requires 1' type argument(s)

Show more comments
avatar image Furiion · Mar 22, 2017 at 01:19 PM 0
Share

@ShadyProductions

This is my PauseGame.cs, Pause() is the root of all this fun, the one I'm trying to call from the other script;

  using System.Collections;
  using System.Collections.Generic;
  using UnityEngine;
  using UnityEngine.UI;
  using UnityEngine.Scene$$anonymous$$anagement;
  
  public class PauseGame : $$anonymous$$onoBehaviour {
  
      public Transform canvas;
      public Canvas credit$$anonymous$$enu;
      public Canvas pause$$anonymous$$enu;
      public Button creditText;
  
      void Start () {
          credit$$anonymous$$enu = credit$$anonymous$$enu.GetComponent<Canvas>();
          pause$$anonymous$$enu = pause$$anonymous$$enu.GetComponent<Canvas>();
          creditText = creditText.GetComponent<Button>();
          credit$$anonymous$$enu.enabled = false;
      }
      
      void Update () {
          if (Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.Escape)) {
              Pause();
          }
      }
  
      public void Pause() {
          if (canvas.gameObject.activeInHierarchy == false) {
                  canvas.gameObject.SetActive(true);
                  Time.timeScale = 0;
          } else {
              canvas.gameObject.SetActive(false);
              Time.timeScale = 1;
          }
      }
  
      public void CreditPress() {
          credit$$anonymous$$enu.enabled = true;
          pause$$anonymous$$enu.enabled = false;
      }
      public void ReturnPress() {
          credit$$anonymous$$enu.enabled = false;
          pause$$anonymous$$enu.enabled = true;
      }
      public void RestartGame() {
          Scene$$anonymous$$anager.LoadScene(Scene$$anonymous$$anager.GetActiveScene().name);
          Pause();
      }
      public void ExitGame() {
          Application.Quit();
      }
  }


avatar image ShadyProductions Furiion · Mar 22, 2017 at 01:23 PM 0
Share

Are both scripts on the same object? and share your script that you are calling the pause on

avatar image Furiion ShadyProductions · Mar 22, 2017 at 01:25 PM -1
Share

Yeah had trouble posting it sorry!

avatar image Furiion Furiion · Mar 22, 2017 at 01:23 PM 0
Share

@ShadyProductions And this is the script I'm attempting to call it from, down the bottom is what you're after;

using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; using UnityEngine.Scene$$anonymous$$anagement;

  public class Player : $$anonymous$$onoBehaviour {
  
      public float moveSpeed = 50.0f;
      private Vector3 position;
      public GameObject projectile; 
      public float fireRate = 0.15f;
      private float fireTime;
      public GameObject deathEffect;
      [SerializeField]
      private Stat healthStat;
      
      public Transform pausegame;
  
      void Start () {
          healthStat.Initialize();
      }
      
      void Update () {
          position = transform.position;
          $$anonymous$$ovement();
          Boundary();
          transform.position = position;
          Shoot();
      }
  
      void Awake (){
          //GetComponent<PauseGame>().Pause();
      }

      public void takeDamage(float damage) {
          healthStat.CurrentVal -= damage;
          if (healthStat.CurrentVal <= 0) {
              Destroy(this.gameObject);
              Instantiate(deathEffect, transform.position,transform.rotation);
              //PauseGame pausegame = GetComponent(); pausegame.Pause();
              GetComponent<PauseGame>().Pause();
          }
      }
  
      public void giveHealth(float healthIncrease) {
          healthStat.CurrentVal += healthIncrease;
          }
  }

Apologies if I've done something blatantly wrong, I really appreciate the help and your time. (I had to erase a good chunk of this script as it wouldn't let me submit the comment. None of it related to what we're doing)

avatar image ShadyProductions Furiion · Mar 22, 2017 at 01:28 PM 0
Share

I see you have a public Transform pausegame; which interferes with the variable you are trying to set when using my 2nd example.

note: technically GetComponent<PauseGame>().Pause(); should work, unless the script is not attached to the sameobject the PauseGame script is attached to.

Try Adding a debug in your pause method to see if u enter the method, you can then see in the console if it writes test:

 Debug.Log("Test!");
Show more comments
avatar image Furiion · Mar 22, 2017 at 01:53 PM 0
Share

@ShadyProductions Thanks a lot for your time and help!! And your patience, pretty obvious mistake now that it's been pointed out. I really appreciate it though thank you. @CesarNascimento Thank you very much! I should've known really, new to all this but it's blatantly obvious. $$anonymous$$uch,much appreciated.

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

The type or namespace "Target" could not be found? 1 Answer

Can't download assets in unity asset store 0 Answers

Keep getting this error message,Hi whenever i open the editor i keep getting this error. 0 Answers

Why have error: NullReferenceException: Object reference not set to an instance of an object 1 Answer

Error "OnTriggerEnter: this message has to be of type: Collider" 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