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 Jammer3000 · Jun 18, 2014 at 08:14 PM · javascriptspawnchild

GetComponentInChildren(Renderer).active wont work?

Hi in one of my scripts this part, the other.gameObject.GetComponentInChildren(Renderer).active = false; line works great but in this script(script below) that same line doesn't work and it gives me the error below. And it doesn't reactivate the child renderer when it runs the if statement(note the line right above it works so it is getting inside the if statement)

 function RespawnDoublePoints () 
 {
     for (var i : int = 0; i < doublePointsClonesArray.Length; i++) 
     {            
        if (doublePointsClonesArray[i].transform.position.x < doublePointsResetPosition)
        {
          doublePointsClonesArray[i].transform.position = gameObject.transform.position + Vector3(Random.Range(4, 15), Random.Range(-4, 4), 0);
          doublePointsClonesArray[i].gameObject.GetComponentInChildren(Renderer).active = true;
 
        }
     }
  
 }

The part not working:

doublePointsClonesArray[i].gameObject.GetComponentInChildren(Renderer).active = true;

Error:

NullReferenceException: Object reference not set to an instance of an object Double Points Spawner Script.RespawnDoublePoints () (at Assets/Scripts/Double Points Spawner Script.js:38) Double Points Spawner Script.Update () (at Assets/Scripts/Double Points Spawner Script.js:22)

Comment
Add comment · Show 1
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 robertbu · Jun 19, 2014 at 01:40 AM 1
Share

If you just want to enable/disable the renderer, then use Renender.enabled:

 var rend = doublePointsClonesArray[i].gameObject.GetComponentInChildren(Renderer);
 if (rend != null) {
     rend.enabled = true;
 }

2 Replies

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

Answer by Jammer3000 · Jun 19, 2014 at 01:44 PM

I found a work around I just made another array and gave the child objects tags and filled that separate array with those objects. Thanks @robertbu and @Eric5h5

  function Start () 
     {
         for (var j : int = 0; j < 2; j++) 
         {
             array[0] = GameObject.FindGameObjectWithTag("DoublePointsChildGlow"); 
             array[1] = GameObject.FindGameObjectWithTag("DoublePointsChildCore");
         }
         
         
          
     }
      
      
     function Update () 
     {
          RespawnDoublePoints();
     }
      
      
     function RespawnDoublePoints () 
     {
     
     for (var j : int = 0; j < array.Length; j++) 
         {        
            if (array[j].transform.position.x < doublePointsResetPosition)
            {
              array[j].gameObject.active = true;
            }
         }
      
     }

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
0

Answer by Eric5h5 · Jun 18, 2014 at 08:47 PM

It means the component doesn't exist. Check for whether the component is null first before you try to do anything with it. By the way, .active is deprecated; you should be using SetActive().

Comment
Add comment · Show 2 · 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 Jammer3000 · Jun 19, 2014 at 01:28 AM 0
Share

Could you give me an example of how to use SetActive()?

avatar image Eric5h5 · Jun 19, 2014 at 01:51 AM 1
Share

http://docs.unity3d.com/ScriptReference/GameObject.SetActive.html

Although that's actually not the thing to use here anyway; active is only for GameObjects, not components.

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

22 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

Related Questions

How to add a peaking system? 1 Answer

Can someone help me fix my Javascript for Flickering Light? 6 Answers

Setting Scroll View Width GUILayout 1 Answer

How Do I Make A Health Bar 6 Answers

GUI.Box that will appear for 3sec 1 Answer


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