- Home /
Camera Switch Error. Code error says The left-hand side of an assignment must be a variable, a property or an indexer
Here's the code I don't see what is wrong,
using UnityEngine;
using System.Collections;
public class CameraSwitch : MonoBehaviour
{
public GameObject camera1;
public GameObject camera2;
void Start ()
{
camera1.SetActive(true);
camera2.SetActive(false);
}
void Update ()
{
if((Input.GetButtonUp("Camera") = true) & (camera1.SetActive(true)))
{
camera1.SetActive = false;
camera2.SetActive = true;
}
else if((Input.GetButtonUp("Camera") = true) & (camera1.SetActive(false)))
{
camera1.SetActive = true;
camera2.SetActive = false;
}
}
}
I'm sure it should work, but it doesn't
Thank you for any help.
Answer by Jamora · Jul 30, 2013 at 12:40 AM
You've forgotten the second = form the equality check in your ifs. Same applies to your and operation.
Edit:
The euqality operator is == , this checks if the left side and the right side are equal. For value types this means if their values are euqals, for reference types it means if their references are equals. This operator always returns true or false. Example: x == 2;
is read as: "does x equal 2?"
The assignment operator = means to set the right hand side to the left hand side. i.e. x = 2;
means "set x to 2;".
The & is a bitwise AND operation, while && is the normal AND.
So in your code you are telling the code basically, "if (set the button for Camera is released to true and add set camera1 to false). Which doesn't make any sense.
Your code should instead read : "if (the button for Camera is released and camera1 is active)"
if((Input.GetButtonUp("Camera") == true) && (camera1.enabled == true))
I'll let you apply similar changes to the rest of your ifs.
Like this?
using UnityEngine;
using System.Collections;
public class CameraSwitch : $$anonymous$$onoBehaviour
{
public GameObject camera1;
public GameObject camera2;
void Start ()
{
camera1.SetActive(true);
camera2.SetActive(false);
}
void Update ()
{
if(((Input.GetButtonUp("Camera") = true)= true) & ((camera1.SetActive(true))= true))
{
camera1.SetActive = false;
camera2.SetActive = true;
}
else if(((Input.GetButtonUp("Camera") = true) & ((camera1.SetActive(false))= true)))
{
camera1.SetActive = true;
camera2.SetActive = false;
}
}
}
It didn't seem to get rid of my error
You're correct, I should've been more precise in my answer.
thanks, it also helps when you use cameras ins$$anonymous$$d of gameobjects.
Your answer
Follow this Question
Related Questions
Distribute terrain in zones 3 Answers
Cinemachine LensSettings.cs Errors? 0 Answers
RenderTexture and Readpixels android problem. 2 Answers
Raycast doesn't collide as it should 1 Answer
Multiple Cars not working 1 Answer