Hi guys, I’ve been playing about with scripting for the past few days, I’m very new to it.
I’m wondering whether I’d be better doing this through a variable or creating an array.
My Scene consists of 3 spheres, a camera and 3 UI buttons. Currently they use two gameobjects
public GameObject ThisPanorama;
public GameObject TargetPanorama;
When I click a button, it sets a new position for the camera based on ‘TargetPanorama’.
public void SetCamera(Vector3 position, Vector3 direction)
{
Camera.position = position;
//Camera.LookAt(direction);
}
It then activates the target pano and hides the current.
private void SetSkyBox()
{
if (TourManager.SetCameraPosition != null)
TourManager.SetCameraPosition(TargetPanorama.transform.position, ThisPanorama.transform.position);
TargetPanorama.gameObject.SetActive(true);
ThisPanorama.gameObject.SetActive(false);
}
The problem being if I click on the third button, the data stored in ‘ThisPanorama’ is wrong and it messes up the position of the camera.
The data in the pano can only be assigned to one sphere, I need to do something like apply an updating variable after each button click explaining what the current sphere is.
With my initial question being relevant, would it be possible to change the class in run-time?
You can’t change scripts, but you can reassign values to variables. Honestly, if you are connecting two gameobjects, you could have a script on one that has a reference to it’s connecting gameobject.
Or you could have a list/array of a class that has the two gameobjects as variables (I prefer this over trying to maintain two list of objects that are suppose to be connected).
Generally speaking, I try to use list or arrays when I’m doing things that are collections. If you find yourself wanting to do.
thisPanorama1
thisPanorama2
thisPanorama3
Then an array would handle this better.
Thank you for your reply, I played around a bit with the suggestions. It confused me a little in areas.
Due to this I decided to simplify the way I was doing things, before it worked around camera movement. I’ve switched this for an instantiating system where I click the button it will tell unity to select the correct sphere, instantiate it and destroy the other instantiated objects.
What I’m a little stuck at is telling each button what list item it refers to?
public class SphereManager : MonoBehaviour
{
public GameObject ButtonID;
public GameObject[] Panosphere;
public Button ThisButton;
private void Start()
{
Button btn = ThisButton.GetComponent<Button>();
btn.onClick.AddListener(OnPanoChange);
Instantiate(Panosphere[0], new Vector3(0f, 0f, 0f), Quaternion.identity);
}
public void OnPanoChange()
{
SetSkyBox();
}
private void SetSkyBox()
{
Instantiate(Panosphere[1], new Vector3(0f, 0f, 0f), Quaternion.identity);
Destroy(Panosphere[others]);
}
}
Here’s what I have so far…
I want the buttonID to reference the correct array, I expect I need to do this in an external script? As I probably want the list on the manager gameobject and the button id on each individual camera?
Okay I sort of have a solution for what I was trying to achieve… and it was simple to apply. I’m not sure if its the most efficient way to do it, but it works at-least.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class Identifier : MonoBehaviour {
public GameObject[] MySpheres;
public Button ThisButton;
// Use this for initialization
void Start () {
Button btn = ThisButton.GetComponent<Button>();
btn.onClick.AddListener(Transition);
}
public void Transition()
{
MySpheres[0].SetActive(true);
MySpheres[1].SetActive(false);
MySpheres[2].SetActive(false);
}
}
I did want to store the array in a manager object and have static values that I just reference to turn on the correct value in an external script on button press but I can’t work that out yet so this is a good temporary fix, if albeit untidy as I have to assign the other panoramas in every button rather than just one.