Cannot implicitly type Vector2 to camera control
so, im very very new to this, im trying to do a simple platformer and the code im trying to make is a simple small piece of code that I can attach to trigger objects throughout the level so that as the player character moves through them it changes to position of the camera. this... was a harder feat than making the character move, it turned out. the piece im having trouble with is the bolded bit.
using UnityEngine;
using System.Collections;
public class Environmenttriggercamera : MonoBehaviour
{
public CameraControl windowOffset;
public GameObject Player;
void Start ()
{
GetComponent<CameraControl> ();
}
void OnTriggerEnter (Collision Collider)
{
if (Collider.gameObject.GetComponent<LayerMask>() == LayerMask.NameToLayer("Player"))
{
**windowOffset = new Vector2 (9,-3);**
}
}
}
the window offset in the camera control script is this right here:
public Vector2 windowOffset = new Vector2(-9,-3);
And no matter what I do it keeps giving me the error "Cannot implicitly convert type 'unityEngine.Vector2' to 'cameracontrol'". I've worked through problems in other parts of this project before, but i've always managed to figure out a solution in the end. this, however, has turned into a straight up roadblock for the past week. can anyone help?
Did you try making the camera a Camera object ins$$anonymous$$d of CameraControl? Then you can set the Camera.transform.position
Answer by Landern · Feb 10, 2016 at 03:32 PM
if "windowOffset" is a field of the type/class CameraControl then you need to access it through the dot operator. Clearly in your code above in the "Environmenttriggercamera" class you a same named field.
try changing:
windowOffset = new Vector2 (9,-3);
to
windowOffset.windowOffset = new Vector2 (9,-3); // you named your instance of CameraControl the same as the field you're trying to set to a Vector2.
Also the error clearly states that you're trying to set a CameraControl type to a Vector2, this is an indication that you're trying mix types that are not interchangeable. A good idea is to name the CameraControl field to something more inline with the type.