Question by
cxs13b · Mar 06, 2018 at 07:24 PM ·
prefabmaterialraycastingskybox
Prefab Undefined Behavior?
I'm working on a Virtual Tour of sorts, and am having trouble accessing a certain public variable in my prefab (the skybox material that will be applied to the camera, associated with the waypoint). I correctly change the camera position, but the skybox material is applying incorrectly (it seems to be applying the skybox of the last instantiated prefab in the scene?). Is this a problem with raycasting, my script, or am I missing something. Thanks for the help. Waypoint Script below:
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class WaypointController : MonoBehaviour {
public Camera mainCamera;
public Material skyboxMaterial;
public GameObject[] VisibleWaypoints;
private Skybox mainCameraSkybox;
private CameraController cameraControllerScript;
void Start(){
mainCameraSkybox = mainCamera.GetComponent<Skybox> ();
cameraControllerScript = mainCamera.GetComponent<CameraController> ();
}
void Update(){
if (Input.GetButtonDown("Fire1")){ //if mouse is down
RaycastHit hit;
Ray ray = mainCamera.ScreenPointToRay(Input.mousePosition); //send out raycast
if (Physics.Raycast(ray, out hit)) {
if (hit.collider.CompareTag("Waypoint")){ //if hits a waypoint
Debug.Log(hit.transform.name);
ApplySkybox(skyboxMaterial);
MoveNewWaypoint (hit.transform);
//update all visable waypoints
}
}
}
}
void ApplySkybox(Material newSkybox){
mainCameraSkybox.material = newSkybox;
Debug.Log ("This skybox belongs to" + transform.name);
}
void MoveNewWaypoint(Transform newTransform){
mainCamera.transform.position = newTransform.position;
//Set new target for CameraController Script
cameraControllerScript.SetTarget (newTransform);
Debug.Log ("This transform.position belongs to" + newTransform.name);
}
void UpdateVisibleWaypoints(){
}
}
Comment