- Home /
GameObject doesn't turn on when renderer camera is visible.
Hello community.
Like the title says and there's the code.
using UnityEngine;
using System.Collections;
public class AutoActivarScript : MonoBehaviour {
private bool aparecio;
// Use this for initialization
void Start () {
CambiarEstado(false);
gameObject.SetActive(false);
}
// Update is called once per frame
void Update()
{
// Comprobamos si ya aprecio en la camara
if (aparecio == false)
{
if (renderer.IsVisibleFrom(Camera.main))
{
CambiarEstado(true);
}
}
else
{
if (renderer.IsVisibleFrom(Camera.main) == false)
{
Destroy(gameObject);
}
}
}
private void CambiarEstado(bool estado)
{
aparecio = estado;
gameObject.SetActive(estado);
}
}
Thanks for any help.
Regards.
Answer by Ryujose · Oct 21, 2014 at 02:22 AM
Now I'm trying to do it more specific and I'm enabling and disabling the components but it drops me an error.
NullReferenceException: Object reference not set to an instance of an object AutoActivarScript.CambiarEstado (Boolean estado) (at Assets/Scripts/AutoActivarScript.cs:50) AutoActivarScript.Update () (at Assets/Scripts/AutoActivarScript.cs:32)
using UnityEngine;
using System.Collections;
public class AutoActivarScript : MonoBehaviour {
private bool aparecio;
private SpriteRenderer spriteRenderer;
private Animator animator;
// Use this for initialization
void Start () {
CambiarEstado(false);
spriteRenderer = GetComponent<SpriteRenderer>();
animator = GetComponent<Animator>();
spriteRenderer.enabled = false;
animator.enabled = false;
}
// Update is called once per frame
void Update()
{
// Comprobamos si ya aprecio en la camara
if (aparecio == false)
{
if (renderer.IsVisibleFrom(Camera.main))
{
CambiarEstado(true);
}
}
else
{
if (renderer.IsVisibleFrom(Camera.main) == false)
{
Destroy(gameObject);
}
}
}
private void CambiarEstado(bool estado)
{
aparecio = estado;
spriteRenderer.enabled = estado;
animator.enabled = estado;
}
}
Edit and solved.
using UnityEngine;
using System.Collections;
public class AutoActivarScript : MonoBehaviour {
private bool aparecio;
// private Renderer spriteRenderer;
// private Animator animator;
void awake()
{
//spriteRenderer = GetComponent<Renderer>();
//animator = GetComponent<Animator>();
}
// Use this for initialization
void Start () {
CambiarEstado(false);
}
// Update is called once per frame
void Update()
{
// Comprobamos si ya aprecio en la camara
if (aparecio == false)
{
if (renderer.IsVisibleFrom(Camera.main))
{
CambiarEstado(true);
}
}
else
{
if (renderer.IsVisibleFrom(Camera.main) == false)
{
Destroy(gameObject);
}
}
}
private void CambiarEstado(bool estado)
{
aparecio = estado;
renderer.enabled = estado;
}
}
Answer by tanoshimi · Oct 20, 2014 at 09:58 PM
You're setting the GameObject on which this script lies to be inactive on Start() (line 12, and again on line 13).
Scripts won't execute on inactive objects, so the isVisibleFrom test in Update() will never get called.
Rather than setting the GameObject inactive, did you mean perhaps to just disable the renderer component? ( I.e. make it invisible?)
There's a way to disable all components and enable all again, and don't turn off the GameObject? I want to do it to assure a good performance on all devices.
Thats why I'm using isVisibleFrom on renderer camera.
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
How to make the camera is not break trough object? 2 Answers
GUI controlling other game objects 0 Answers
Camera rotate around sphere centre 2 Answers
c# destroy gameobject on 0 hp 2 Answers