Can't change camera position

So I have some code that is meant to move the camera.

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

public class CameraUp : MonoBehaviour
{
    public GameObject Camera;
    public float CameraY;
    public float CameraYNew;
    public Vector2 CameraPos;

    void Update()
    {
        CameraY = Camera.transform.position.y;
        CameraYNew = CameraY + 0.5f;
        CameraPos = Camera.transform.position;
    }



    public void CameraGoesUp()
    {
       
        CameraPos = new Vector2(0, 56);
    }

   
}

There are no errors in the error box, I made sure to define Camera, and I don’t think there’s anything in the MainCamera Inspector that locks/unlocks the Camera. Could I have some help?

All you are doing is setting the values of a Vector2 and a float on your custom component.

You need to assign those values to Camera.transform.position or the camera’s position won’t change.

Well, I’m getting the variables from the camera
( ```

  • public GameObject Camera;

And I set the Main Camera to that camera, so when I set the variable for Camera to a different value, the actual Camera should move too. However, obviously, this isn't working and I have no idea why.

But you have nowhere in that code that sets the values to the camera, so of course it will never move. All the code you have is reading values from the camera, and adding to these variables. Doing that does not magically set any values on the camera. You have to update the Camera.transform.position, but all you seem to be doing is reading from it.

Well, how do I update it? I’ve tweaked the code now so that the GameObject directly is being changed, but it still doesn’t work.

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

public class CameraUp : MonoBehaviour
{
    public GameObject Camera;
    public float CameraY;
    public float CameraYNew;

    void Update()
    {
        CameraY = Camera.transform.position.y;
        CameraYNew = CameraY + 0.5f;
    }



    public void CameraGoesUp()
    {

        Camera.transform.position = new Vector2(0, CameraYNew);
    }

   
}

have you tried calling your method CameraGoesUp() in your update?

void Update()
{
CameraY = Camera.transform.position.y;
CameraYNew = CameraY + 0.5f;
CameraGoesUp();
}

TLDR? The previous answer is the correct one.

I sincerely hope you don’t take offense at this, it’s not my intention but the problem I see with your question here and several of your previous ones is that you’re asking about things like Camera and positions but your code changes are showing that you fundamentally don’t understand the basics of C# and the code you’re writing. This in itself isn’t a problem or criticism of you as everyone has to learn this stuff, but it’s certainly a problem when you are asking a specific question without the basic knowledge to understand what you’re being told. This is essentially running before you can walk but it’s your choice in the end.

Personally, I would strongly suggest following the Beginner Scripting course on Unity Learn. It’s free and it will remove a whole bunch of frustration for you. In the very least it will expose you to better terminology and allow you to ask better questions. It’s certainly more fun to write code without stopping for minor things like the above. :slight_smile:

I actually have this script in a button, and have made the button call the method CameraGoesUp(). This works for other things (I have some code exactly like this that works just fine) but not the camera. (Also I will check out the Beginner Scripting course.)