- Home /
Question by
Camkash · Sep 02, 2014 at 01:16 PM ·
c#camerainstantiateinstance
`UnityEngine.Camera.main' cannot be accessed with an instance reference, qualify it with a type name instead
New to C#, it's pedantic. I'm used to Matlab. Basically `UnityEngine.Camera.main' cannot be accessed with an instance reference, qualify it with a type name instead. In other words, it makes too much sense to be able to access the main camera by saying camera.main. How do I fix this?
using UnityEngine; using System.Collections;
public class Building : MonoBehaviour { public GameObject sphere1=null;
// Use this for initialization
void Start () {
sphere1=Instantiate(Resources.Load("Prefabs/prefab_sphere")) as GameObject;
}
// Update is called once per frame
void Update () {
var playerObject = GameObject.Find("Player");
var playerPos = (Vector3)playerObject.transform.position;
// var cam = camera.GetComponent(main);
var buildingPos = playerPos + camera.main.WorldToScreenPoint(Vector3.zero);
Debug.Log(playerPos);
if(Input.GetMouseButtonDown(0))
sphere1=Instantiate(Resources.Load("Prefabs/prefab_sphere"),buildingPos,Quaternion.identity) as GameObject;
}
}
Comment
Best Answer
Answer by AyAMrau · Sep 02, 2014 at 01:18 PM
It's Camera.main (capital C) - to access the public static on the Camera class which refers to the Main Camera.
camera is a property returning a camera attached to the same gameObject as the script.
In C# static members are accessed using the class name and not instance names.
Your answer
![](https://koobas.hobune.stream/wayback/20220613162122im_/https://answers.unity.com/themes/thub/images/avi.jpg)