I'm trying to display telephone camera in two scenes using WebCamTexture but when I load second scene in my android device the game crashes. I've created a plane (as a cinema screen) in front the camera and I've attached this script:
using UnityEngine;
using System.Collections;
public class CameraController : MonoBehaviour
{
public WebCamTexture mCamera;
public GameObject plane;
void Start ()
{
plane = GameObject.FindWithTag ("PlayerCam");
mCamera = new WebCamTexture ();
plane.GetComponent<Renderer>().material.mainTexture = mCamera;
mCamera.Play ();
}
}
In unity editor all works ok, but when I load second scene my android device crashes. Can anybody help me?
You need to destroy the WebCamTexture before loading another scene
Related
Im trying to make a system in which my UI will display the nearest enemys health and sprite. I have already got the panels and UI working to display the Player health but im unsure as to how to display whichever enemy has came close or within range of the player.
** The game is running around but coming within range of an enemy triggers combat**
The below code can display a characters health if I place the script on the player/enemy. Im grabbing two variables from Bolt also.
I can display a single enemy no problem in this way. But if i put 50 enemies in one scene it means I have to manually load the panels/UI for each enemy.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Bolt;
using Ludiq;
using UnityEngine.UI;
public class HealthAndCombat : MonoBehaviour
{
public GameObject player;
public Slider healthBarSlider;
public float health;
private bool inCombat;
private GameObject nearestEnemy;
public GameObject healthBarCanvas;
public GameObject bestFighterUI;
private void Start()
{
healthBarSlider.value = health;
bestFighterUI.SetActive(false);
}
void Update()
{
health = (float)Variables.Object(player).Get("Health");
inCombat = (bool)Variables.Object(player).Get("combat");
nearestEnemy = (GameObject)Variables.Object(player).Get("nearestEnemy");
if (inCombat)
{
healthBarCanvas.SetActive(true);
bestFighterUI.SetActive(true);
healthBarSlider.value = health;
}
else {
healthBarCanvas.SetActive(false);
bestFighterUI.SetActive(false);
}
}
Im thinking -
-Load the scene.
-Find all objects called enemy and register their health in a list.
-Work out distance from player to each enemy.
-If player in combat, display nearest enemy to player.
I just dont know should I have an overall scene manager for code or have scripts for detection on each player.
I'm tryinng to change the camera position by a GameManager script, but having an error like this:
< MissingComponentException: There is no 'Camera' attached to the "Game Manager" game object, but a script is trying to access it.
You probably need to add a Camera to the game object "Game Manager". Or your script needs to check if the component is attached before using it. >
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class GameManager: MonoBehaviour
{
public GameObject maincamera;
public void Awake ()
{
maincamera = GameObject.Find("Camera");
maincamera.transform.position = new Vector3(1, 1, 1);
}
}
Can someone explain what i get wrong and how can i fix this?
camera transform
I am trying to move my camera based on the players' movements on Y axis in Unity.
However, it does not work...
What did I do wrong? I have attached image of my script (C#) here.
and, Yes, I did attach this script with Main Camera.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CameraController : MonoBehaviour {
GameObject player;
// Use this for initialization
void Start () {
this.player = GameObject.Find("cat");
}
// Update is called once per frame
void Update () {
Vector3 playerPos = this.player.transform.position;
transform.position = new Vector3(
transform.position.x, playerPos.y, transform.position.z);
}
}
Make the player GameObject public and just drag and drop your player in the inspector in unity see if that works? Are you getting any exceptions? Also add Debug.Log (player.transform.position.ToString ()) to see if it is showing the right values. Are you sure you player object name is cat and not Cat, it is case sensitive. Check on those things and let me know if you figured it out!
I'm working on a 2D UI application with Unity and i have a problem.
I was working on the script to instantiate my popup window (i made a prefab). And i succeed but later Unity crashed and i had to redo my scene (forgot to ctrl+s)
Since this crash, my popup isn't instantiate as a child of my canvas and i've got this message:
"Setting the parent of a transform which resides in a prefab is disabled to prevent data corruption"
Here is my script :
using UnityEngine;
using System.Collections;
public class Popup : MonoBehaviour
{
public RectTransform popupPrefab;
private Animator anim;
// Use this for initialization
void Start()
{
//get the animator component
anim = popupPrefab.GetComponent<Animator>();
//disable it on start to stop it from playing the default animation
anim.enabled = false;
}
public void CreatePopup()
{
// Copie du prefab
RectTransform newPopup = Instantiate(popupPrefab, popupPrefab.transform.position, popupPrefab.transform.rotation) as RectTransform;
newPopup.transform.SetParent(transform, false);
//anim = newPopup.GetComponent<Animator>();
//anim.enabled = true;
//anim.Play("Popup");
}
public void ClosePopup()
{
anim.enabled = true;
anim.Play("ClosePopup");
}
}
I don't understand why i have this error since it was working fine before crash...
if you have any idea
Thanks
I solved the problem by re-doing my project from the beginning...
I'm looking to create a piece of terrain in unity using only a script (c# preferably) to do this rather than the menu options on the editor. So far I only have this code below, but I don't know what to do next to get it to appear on the scene, can anyone help?
Thank you
using UnityEngine;
using System.Collections;
public class terraintest : MonoBehaviour {
// Use this for initialization
void Start () {
GameObject terrain = new GameObject();
TerrainData _terraindata = new TerrainData();
terrain = Terrain.CreateTerrainGameObject(_terraindata);
}
// Update is called once per frame
void Update () {
}
}
Simply adding :
Vector3 position = ... //the ingame position you want your terrain at
GameObject ingameTerrainGameObject = Instantiate(terrain, position, Quaternion.identity);
should make the terrain appear ingame. The Instantiate method returns a reference to the gameobject spawned ingame, so if you later want to access it, you can use that reference.