Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Ryujose · Oct 20, 2014 at 09:22 PM · cameragameobjectrendererboolean

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.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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;
 
        
        
     }
 }
 
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
1

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?)

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Ryujose · Oct 20, 2014 at 10:15 PM 0
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

27 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges