Dholu
Dholu

Reputation: 697

I want to access a public method from one script to another in Unity. I have tried the below code

I am getting the below error while accessing ObjectPoolingManager.Instance.GetBullet(); in Player.cs from public GameObject GetBulltet () which is in ObjectPoolingManager.cs

Assets\SPF_Assets\Scripts\Game\Player.cs(4,7): error CS0138: A 'using namespace' directive can only be applied to namespaces; 'ObjectPoolingManager' is a type not a namespace. Consider a 'using static' directive instead

Player.cs Script

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using ObjectPoolingManager; //Tried this, didnt work

public class Player : MonoBehaviour
{
    public Camera playerCamera;
    public GameObject bulletPrefab;
    // Update is called once per frame
    void Update() {
        if(Input.GetMouseButtonDown(0)){
            ObjectPoolingManager.Instance.GetBullet();

            GameObject bulletObject = Instantiate (bulletPrefab);
            bulletObject.transform.position = playerCamera.transform.position + playerCamera.transform.forward;
            bulletObject.transform.forward = playerCamera.transform.forward;
        }
    }
}

ObjectPoolingManager.cs

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class ObjectPoolingManager : MonoBehaviour {

    private static ObjectPoolingManager instance;
    public static ObjectPoolingManager Instance { get { return instance; } }

    // Start is called before the first frame update
    void Awake () {
        instance = this;
    }

    public GameObject GetBulltet () {
        Debug.Log ("Hell0");
        return null;
    }
}

Upvotes: 1

Views: 53

Answers (1)

Dominic Frei
Dominic Frei

Reputation: 422

I re-read the code multiple times and finally realised your answer is right in your question already:

I am getting the below error while accessing
ObjectPoolingManager.Instance.GetBullet(); in Player.cs
from public GameObject GetBulltet () which is in ObjectPoolingManager.cs

I verified this in your code and you actually call a function ObjectPoolingManager.Instance.GetBullet() even though there is no such function but instead GetBulltet() which should have been GetBullet() instead.

Upvotes: 1

Related Questions