x


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.

more ▼

asked Jul 30, 2013 at 12:39 AM

DragonHardy gravatar image

DragonHardy
3 3 2 5

(comments are locked)
10|3000 characters needed characters left

1 answer: sort voted first

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.

more ▼

answered Jul 30, 2013 at 12:40 AM

Jamora gravatar image

Jamora
15.1k 6 8 49

Like this?

 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)= 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

Jul 30, 2013 at 12:48 AM DragonHardy

You're correct, I should've been more precise in my answer.

Jul 30, 2013 at 01:19 AM Jamora

thanks, it also helps when you use cameras instead of gameobjects.

Jul 30, 2013 at 01:26 AM DragonHardy
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x10614
x5043
x3696
x54
x34

asked: Jul 30, 2013 at 12:39 AM

Seen: 707 times

Last Updated: Jul 30, 2013 at 04:48 PM