- Home /
Multiple Prefab Instantiation, Camera, Button and Scroll View Question
I am trying to:
Instantiate a prefab Sphere Game Object Instantiate a prefab Camera and assign it as a child of the new prefab Sphere Game Object Instantiate a prefab button that includes the script to switch the camera from the default to new Add the new button to a scroll view canvas object as a child
This is what I have so far:
public Camera CameraPrefab, DefalutCamera;
public Camera[] cameras;
private int currentCameraIndex;
public Transform GenericCanvasScrollViewObject;
public Button ButtonPrefab;
public GameObject SpherePrefab;
` void NewSphereObjectSetup()
{
GameObject newSphere = Instantiate(SpherePrefab, SetSpawnPos, Quaternion.identity);
Camera newCamera = Instantiate(CameraPrefab);
newSphere.transform.parent = newCamera.transform;
Button newButton = Instantiate(ButtonPrefab);
newButton.onClick.AddListener(SwitchToCamera(newCamera));
newButton.transform.SetParent(GenericCanvasScrollViewObject);
}
public UnityAction SwitchToCamera(Camera anyCamera)
{
DefalutCamera.enabled = false;
anyCamera.enabled = true;
}
There are no errors until I get to the SwitchToCamera in the UnityAction. Error not all code paths return a value.
Any ideas? Or Am I on the wrong path altogether? (SetSpawnPos is brought in from another working function, please ignore the script exclusion)
"public UnityAction SwitchToCamera" means that C# is expecting SwitchToCamera to return something of type UnityAction - perhaps replace "UnityAction" with "void", meaning that nothing is returned.
To change camera you might need to use Camera.main, which is the current camera (after you've run this twice, DefaultCamera will no longer be the active camera)
PS would you $$anonymous$$d bullet-pointing what you are trying to do? The wording makes it slightly hard to understand at the moment :P
Answer by AnimaCrucio · Jul 18, 2018 at 11:15 PM
At this point I will start basic and I will add the rest of what I need when I get the a working version of just one.
I need to
instantiate a SphereObjPrefab
instantiate a ButtonObjPrefab
add the instantiated newButtonObjPrefab to an existing Scroll View, Scroll Rect transform
switch the main camera to the newSphereObjPrefab on newButtonObjPrefab on Click()
That is it. The characteristics of the newSphereObjPrefab or newButtonObjPrefab, or even the camera setting don't matter at this point. I just need to make a sphere, click the button, and see it. Any help is greatly appreciated. Thanks for your response.