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.
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?)
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;
}
}