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 melonman · Oct 17, 2011 at 05:51 PM · visibility

simple invisibility

Hi Im using c# was just wondering why a simple effect like toggling off my visibility won't work.

I think it must be me just being really dense and missing something really simple.

Have written the following script and attached it to my cube.

Whenever I load up my game I get a null reference error and the warning

Warning CS0649: Field 'test.Cube' is never assigned to, and will always have its default value null (CS0649) (Assembly-CSharp)

I don't really know what it means by assigned to either usually when I get null references it's because I haven't declared an object etc have done visibility before aswell and looked at my other code which is the same evokes visibility or invisibility by using the objects.renderer.enabled function.

Where have I messed up?

using UnityEngine; using System.Collections;

public class test : MonoBehaviour {

     GameObject Cube;
 
 void Awake ()
     
 {
     GameObject Cube = GameObject.Find("Cube");
     
     
 }
 // Update is called once per frame
 void OnGUI () {
     
     
     if(Event.current.type == EventType.keyUp && Event.current.keyCode == KeyCode.F1)
         
     {
         Cube.renderer.enabled = false;
         
         print("working");
     }
 
 }

}

Comment
Add comment · Show 3
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 cj_coimbra · Oct 17, 2011 at 06:31 PM 0
Share

Being never assigned means that when it reads the input, your GameObject Cube is null and there is no renderer to set false.

Try GameObject.FindGameObjectWithTag("Cube") ins$$anonymous$$d of GameObject.Find("Cube") and put the "Cube" tag on your Cube obviously!

avatar image asafsitner · Oct 17, 2011 at 06:35 PM 0
Share

Why are you even using GameObject.Find to find your cube when the script is attached to it? Use transform.renderer.enabled ins$$anonymous$$d. Also, take it ouf of OnGUI and into Update. You're not using any GUI elements and it causes garbage collector issues.

avatar image Eric5h5 · Oct 17, 2011 at 07:14 PM 0
Share

Just renderer.enabled is sufficient, you don't need transform.renderer.enabled. There isn't a FindGameObjectWithTag command, but FindWithTag isn't a substitute for Find anyway, they do two different things.

1 Reply

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

Answer by Eric5h5 · Oct 17, 2011 at 07:18 PM

You never use the "Cube" variable. Instead you have another local variable defined in Awake, but you never use that either. But as asafsitner mentioned, you don't need to use Find if the script is already attached to the object you want to work with.

As an aside, you'd be better off following the convention of using lowercase for variable names and uppercase for class names. Also, please format your code properly when posting, it takes two seconds.

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 melonman · Oct 17, 2011 at 07:46 PM 0
Share

Thanks guys will get on it and apologies for not formatting code im still quite new to code but will do that in future as well when adressing a problem.

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

How do I see a Configurable Joint in the Scene Window? 0 Answers

Mouse Over Visibility 1 Answer

How can I get the lowest position of objects that are visible by the camera? 1 Answer

Hide cursor and stop interaction 1 Answer

Objects Appearing 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