Hi having issues with with a fucntion to create buttons

Hi I have written a function to create a brand new set of buttons however the function does not seem to draw the buttons
heres the function

 public int chooseMovecamera(int Turn)
    {
        Debug.Log("move camera" + Turn);
        if (Turn == 0)
      {

        if (GUI.Button(new Rect(Screen.width * XCoordinate2, Screen.height * YCoordinate2, Screen.width * 0.5f, Screen.width * 0.025f), "move to Cameloat"))
        {
             return Turn;

        }

        if (GUI.Button(new Rect(Screen.width * XCoordinate3, Screen.height * YCoordinate3, Screen.width * 0.5f, Screen.width * 0.025f), "move to teh holy grail "))
        {
         
            return Turn;
        }

        if (GUI.Button(new Rect(Screen.width * XCoordinate4, Screen.height * YCoordinate4, Screen.width * 0.5f, Screen.width * 0.025f), "move to the knights tournament"))
        {
        
            return Turn;
          
        }

        if (GUI.Button(new Rect(Screen.width * XCoordinate5, Screen.height * YCoordinate5, Screen.width * 0.5f, Screen.width * 0.025f), "move to Lancelotts Quest"))
        {
        
            return Turn;
        }

            return Turn;
      }
            return Turn;
    }

and here is where the function is used:

 {
                    Turn = 0;
                    chooseMovecamera(Turn);
                    Turn = 3;
                    Debug.Log(Turn);

                }
  • Are you sure you’re doing all these into the MonoBehaviour.OnGUI function?
  • Make sure Rects are made from visible coordinates.
  • As an alternative, you could use GUILayout so you don’t have to worry about the Guis locations and let the system organize the layout for you.

thanks but I cant seem to get the function to work so implemented it in a different way. But, I am trying to enable and disable two different cameras but cannot get it to work. I get two errors

  • The name ‘GetComponent’ does not exist in current context
  • The type or namespace name ‘CamelotCamera’ could not be found. Are you missing a using directive or an assembly reference.

The code below:

{
            Camera myCamera;
            myCamera = GetComponenet<ExaliburCamera>(); //get current camera component
            myCamera.enabled = false;                    //disable camera
            myCamera = GetComponent<CamelotCamera>(); //get camera moving to component
            myCamera.enabled = true;                    //enable camera
            //function to clear whole cards in play(if its a single quest).
                
                
                
                
            Turn = 3;

        }

Hi SoulbladeSBA,

It appears that you are trying to assign the “myCamera” variable to components that are not the Camera component.

Here’s an example of what you should be assigning the “myCamera” variable to, if you are not assigning the variable within the inspector:

using UnityEngine;

//Declare the myCamera variable
Camera myCamera;;

void Start()
{
    //Get the camera component on the gameobject
     myCamera = gameObject.GetComponent<Camera>();
}

In order to toggle between two cameras, you must assign each Camera to separate variables, rather than the same one. Example:

using UnityEngine;

//Declare the camera variables. Assign these in the inspector, rather than the script.
Camera firstCamera;
Camera secondCamera;

//Use a bool to toggle between the active cameras.
bool firstCameraOn = false;

void Start()
{
     //Do Nothing
}

void Update()
{
    //If you press the tab button, switch cameras.
     if(Input.GetKeyDown(KeyCode.Tab))
     {
          firstCameraOn = !firstCameraOn;
     }

     //Set the first camera to be active when firstCameraOn is true, and do the opposite for the second camera.
     firstCamera.gameObject.SetActive(firstCameraOn);
     secondCamera.gameObject.SetActive(!firstCameraOn);
}

Hope this helps!

thanks, it helps alot however I’m still getting an issue and I’m wondering if I am trying to accesses cameras wrong, the two cameras are called ExcaliburCamera and CamelotCamera.

     CurrCamera = ExaliburCamera.GetComponent<Camera>();

            CameraChoice1 = CamelotCamera.GetComponent<Camera>();

Make sure that CurrCamera and CameraChoice1 are declaring Cameras and make sure that ExaliburCamera and CameloCamera are GameObjects, like below.

using UnityEngine;
using System;

public class Example : Monobehaviour
{

     //Get these in the Start function
     Camera CurrCamera;
     Camera CameraChoice1;
    
     //Set these in inspector
     public GameObject ExaliburCamera;
     public GameObject CamelotCamera;

     void Start()
     {
          CurrCamera = ExaliburCamera.GetComponent<Camera>();
 
          CameraChoice1 = CamelotCamera.GetComponent<Camera>();
     }
}

okay, so it seems to work however when the current camera I’m using is disabled and the camera I’m moving to is enabled there is just a blank scene saying “scene is missing a full screen camera”

Do both cameras have camera components? Also, can you post your full code, please? This’ll help us find out if the problem is in the inspector or within the code.

Thanks!

yes, both cameras have camera components however the Camelot camera is set to disabled at the beginning of the scene in the inspector.

using UnityEngine;
using System.Collections;
 void OnGUI()
  {
public class ExcaliburQuest_U_I : MonoBehaviour
{
    // information of quest
    public int QuestID = 1; //ID for Quest Exalibur



    public GameObject ExaliburCamera;
    public GameObject CamelotCamera;
  
    // variables for deciding quest movements
    string option1;
    string option2;
    string option3;
    string option4;
    string option5;

   Camera CurrCamera;
   Camera CameraChoice1;
   Camera CameraChoice2;
   Camera CameraChoice3;
   Camera CameraChoice4;

int Turn = 0;

  //sets options for movment of quest
  if (QuestID == 1)
  {
  option1 = "Camelot";
  option2 = "Holy Grail";
  option3 = "Knights Tournament";
  option4 = DragonLancelot;
  CurrCamera = ExaliburCamera.GetComponent<Camera>();
  CameraChoice1 = CamelotCamera.GetComponent<Camera>();
  //CameraChoice2;
  //CameraChoice3;
  // CameraChoice4;
  
  
  }

   if (Turn == 0)
        {
             if (GUI.Button(new Rect(Screen.width * XCoordinate2, Screen.height * YCoordinate2, Screen.width * 0.5f, Screen.width * 0.025f), "move to " + option1))
        {
         
           CurrCamera.gameObject.SetActive(false);//disable current camera
           CameraChoice1.gameObject.SetActive(true);                    //enable camelot camera to move to camelot
            //fucntion to clear whote cards in play(if its a single quest).
               
               
               
               
            Turn = 3;

        }

        if (GUI.Button(new Rect(Screen.width * XCoordinate3, Screen.height * YCoordinate3, Screen.width * 0.5f, Screen.width * 0.025f), "move to " + option2))
        {
          
          
          
          
            Turn = 3;
         
        }

        if (GUI.Button(new Rect(Screen.width * XCoordinate4, Screen.height * YCoordinate4, Screen.width * 0.5f, Screen.width * 0.025f), "move to " + option3))
        {

         
          
          
          
            Turn = 3;
          
        }

        if (GUI.Button(new Rect(Screen.width * XCoordinate5, Screen.height * YCoordinate5, Screen.width * 0.5f, Screen.width * 0.025f), "move to " + option4))
        {
          
          
          
          
            Turn = 3;
          
        }

         
      }
}
}

You don’t appear to be assigning CurrCamera or CameraChoice1 to anything. Assuming you are assining the “ExaliburCamera” and “CamelotCamera” GameObjects in the inspector: try replacing this…

           CurrCamera.gameObject.SetActive(false);//disable current camera
           CameraChoice1.gameObject.SetActive(true);

with this…

           ExaliburCamera.SetActive(false);
           CamelotCamera.SetActive(true);

hi sorry I missed a massive chunk in my code I have, I assumed this assigned it put it up in the code but here it is again

  //sets options for movment of quest
  if (QuestID == 1)
  {
  option1 = "Camelot";
  option2 = "Holy Grail";
  option3 = "Knights Tournament";
  option4 = DragonLancelot;
  CurrCamera = ExaliburCamera.GetComponent<Camera>();
  CameraChoice1 = CamelotCamera.GetComponent<Camera>();
  //CameraChoice2;
  //CameraChoice3;
  // CameraChoice4;


  }

I tried this but the same error occoured

sorry, I have solved the issue however this has confused me on something. When an object has a component that is unticked does this mean it is disabled or non-existent.

Fantastic!

If a component’s tickbox is not checked the component is disabled.

I know the issues solved just a little bit of curiosity -then why would it not enable the object when it was disabled?

Was the code to enable it on the object itself? Disabled objects won’t receive any callbacks, such as Start or Update.