- Home /
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.
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
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)
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.
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!
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();
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)
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();
}
}
Are both scripts on the same object? and share your script that you are calling the pause on
@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)
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!");
@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
Follow this Question
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