Hi guys, I have a problem with the GUITexture. I used .active rather than .enabled because when I switch the camera while the GUITexture is on screen, it will still displayed even I have switched the camera. But when I inactive it .active = false;
and want to activate again .active = true
, it did not have any effect (the GUITexture remains inactive). How would I active it again after inactive?
Here is the some screenshots:
[36127-image.jpg*_|36127]
Here is the bigger image of the GUITexture:
[36128-image+2.jpg*_|36128]
As you can see on the above image, the first time I trigger the box, it display the warning sign above the player, once the warning sign appear and I hit enter, it goes to the first time - enter, once I hit escape from the first time - enter, it goes back to where the first time is, but once I trigger the box again, the warning sign won’t appear, as you can see on the second time. I have checked hierarchy for the gui texture that I inactive it, but it still remains inactive while I have code it to be active once hit the escape and once trigger the box again. Inactive it only just user hit enter while triggering the box.
Here is the code that I am using:
using UnityEngine;
using System.Collections;
[RequireComponent(typeof(CharacterController))] // This script requires CharacterController attached to the game object where this script attached into
public class CheckPlayer : MonoBehaviour
{
public Camera[] cameras; // Define the camera array
private Vector3 moveDirection = Vector3.zero; // Define and set for the movement of the player is not moving by default
private float gravity = 20.0f, speed = 5.0f; // Define and set for the gravity, speed of the player
public static bool isOnChat = false; // Determine if the player is on chat mode
private void Start()
{
// Call the SelectCamera function
SelectCamera(0);
// Set isOnChat to false
isOnChat = false;
}
private void Update()
{
// Get the CharacterController component
CharacterController controller = GetComponent<CharacterController>();
// If the character is on the ground and is not on chat
if (controller.isGrounded && !isOnChat)
{
// Get the axis direction for the movement of the character from the Input in the editor
moveDirection = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));
// Player movement depends on the move direction
moveDirection = transform.TransformDirection(moveDirection);
// The player movement is depends on the player speed
moveDirection *= speed;
}
// How much for the time for player takes to hit the ground because of gravity
moveDirection.y -= gravity * Time.deltaTime;
// Move the character each second while pressing the key input defined in the editor
controller.Move(moveDirection * Time.deltaTime);
// If user press escape
if (Input.GetKey("escape"))
{
// Select the main camera
SelectCamera(0);
// Set isOnChat to false
isOnChat = false;
}
// If onTriggeredPotionsShop is true and user press enter
if (Portal.onTriggeredPotionsShop && Input.GetKey(KeyCode.Return))
{
// Select the camera for potions shop
SelectCamera(1);
// Set isOnChat to true
isOnChat = true;
}
}
private void SelectCamera(int index)
{
// Loop through how many camera array we have
for (int i = 0; i < cameras.Length; i++)
{
// If the int i is same with the int index parameter
if (i == index)
{
// Activate only the index selected
cameras*.camera.active = true;*
}
// But if the int i is not same with the int index parameter
else
{
// Deactivate all other cameras in the scene
cameras*.camera.active = false;*
}
}
}
}
Below code is the trigger method:
using UnityEngine;
using System.Collections;
public class Portal : MonoBehaviour
{
public AudioClip audioSEClip = null; // Define the AudioClip and can be assigned through the code and the inspector
public static bool onTriggeredPotionsShop = false
private void OnTriggerEnter(Collider obj)
{
// If the object collides with the tag of Portal Potions
if (obj.gameObject.tag == “Portal Potions”)
{
// Play the audio for one time
audio.PlayOneShot(audioSEClip);
// Set the onTriggeredPotionsShop to true
onTriggeredPotionsShop = true;
}
}
private void OnTriggerExit(Collider obj)
{
// If the object not collided with the tag of Portal Potions
if (obj.gameObject.tag == “Portal Potions”)
{
// Set the onTriggeredPotionsShop to false
onTriggeredPotionsShop = false;
}
}
}
Below code is for the warning sign to follow the player and also inactive or active the warning sign:
using UnityEngine;
using System.Collections;
public class IconFollow : MonoBehaviour
{
public Transform target; // Object that this label should follow
public RaycastHit hit;
public Vector3 offset = Vector3.up; // Units in world space to offset; 1 unit above object by default
public Camera cameraToUse; // Only use this if useMainCamera is false
public bool clampToScreen = false; // If true, label will be visible even if object is off screen
public float clampBorderSize = .05f; // How much viewport space to leave at the borders when a label is being clamped
public bool useMainCamera = true; // Use the camera tagged MainCamera
private Camera cam;
private Transform thisTransform;
private Transform camTransform;
private void Start()
{
thisTransform = transform;
if (useMainCamera)
{
cam = Camera.main;
}
else
{
cam = cameraToUse;
}
camTransform = cam.transform;
}
private void Update()
{
if (CheckPlayer.isOnChat)
{
this.guiTexture.active = false;
}
else
{
this.guiTexture.active = true;
}
}
private void LateUpdate()
{
if (Portal.onTriggeredPotionsShop)
{
this.guiTexture.enabled = true;
ShowIcon();
}
else if (!Portal.onTriggeredPotionsShop)
{
this.guiTexture.enabled = false;
}
}
private void ShowIcon()
{
if (target)
{
var relativePoint = cam.transform.InverseTransformPoint(target.position);
if (clampToScreen)
{
var relativePosition = camTransform.InverseTransformPoint(target.position);
relativePosition.z = Mathf.Max(relativePosition.z, 1.0f);
thisTransform.position = cam.WorldToViewportPoint(camTransform.TransformPoint(relativePosition + offset));
thisTransform.position = new Vector3(Mathf.Clamp(thisTransform.position.x, clampBorderSize, 1.0f - clampBorderSize), Mathf.Clamp(thisTransform.position.y, clampBorderSize, 1.0f - clampBorderSize), thisTransform.position.z);
}
else
{
if (relativePoint.z > 0.0)
{
if (Physics.Raycast(cam.transform.position, (target.position - cam.transform.position), out hit, Mathf.Infinity))
{
if (hit.transform == target)
{
thisTransform.position = cam.WorldToViewportPoint(target.position + offset);
this.guiTexture.enabled = true;
}
else
{
this.guiTexture.enabled = false;
}
}
}
}
}
else
{
Destroy(gameObject, 0.8f);
}
}
}
By the way, I have attached the above script named (IconFollow.cs) on the gameobject called (warning sign) that have the GUITexture attached and this game object children of the gameobject called (character) in the hierarchy, I have attached the (CheckPlayer.cs and Portal.cs) to the gameobject called (character), basically these two script (CheckPlayer.cs and Portal.cs) just for triggering and do the command. For inactive and active is from the IconFollow.cs
Sorry for long posting, hope you guys not too bored to read my question.
Thank you very much!
_*
_*