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 /
This post has been wikified, any user with enough reputation can edit it.
avatar image
0
Question by Potatobob · Apr 05, 2014 at 12:02 AM · c#renderer.enabled

How to whrite in c# the code: onMouseEnter Hide mesh

Hi, I Am writing a code in c# here is what I have at the moment

using UnityEngine; using System.Collections;

public class MouseOver1 : MonoBehaviour {

 void OnMouseEnter(){
     renderer.enabled = false;
 }
 void OnMouseExit(){
     renderer.enabled = true;
     
 }

}


Im recieving those 2 errors when using the mouse over the cube

NullReferenceException: Object reference not set to an instance of an object MouseOver1.OnMouseEnter () (at Assets/MouseOver1.cs:7) UnityEngine.SendMouseEvents:DoSendMouseEvents(Int32, Int32)


NullReferenceException: Object reference not set to an instance of an object MouseOver1.OnMouseExit () (at Assets/MouseOver1.cs:11) UnityEngine.SendMouseEvents:DoSendMouseEvents(Int32, Int32)


The code is indeed attached to the cube or what ever else I try with and the object has a collider. I do not understand what is missing thank you for helping me I am really stuck and I am only at the 1st lines of script in my game :S

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 · Apr 05, 2014 at 04:54 AM 0
Share

Your script works fine for me. I attached it to a cube in a new scene. I imagine you would get this error if you attached it to an empty game object...or any game object without a $$anonymous$$eshRenderer component.

1 Reply

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

Answer by RyanZimmerman87 · Apr 05, 2014 at 02:49 AM

Hmm well surprisingly I still haven't used OnMouseEnter or OnMouseExit even after working with Unity so long... For some reason I thought it was just for GUI stuff but it also says collider in the documentation and it does seem to recognize your commands based on your null reference it just isn't getting access to the object renderer.

You should try creating a game variable like this:

 GameObject cubeObject;
 
 void Start()
 {
 cubeObject = gameObject;
 }
 
 void OnMouseEnter()
 {
     cubeObject.renderer.enabled = false;
 }


That should solve your problem based on the null reference error. Normally I don't think you would need the Game Object variable right in the script though if the script is already on that game object... something seems strange here. Maybe it's something to do with OnMouseEnter. Have you tried just enabling and disabling the renderer with other methods like a timer instead of OnMouseEnter?

Do any other objects in your scene have that OnMouseEnter() in their scripts?

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 Potatobob · Apr 05, 2014 at 01:11 PM 0
Share

Works perfectly thancks for the feedback, I was indeed thinking that I need to specify the target to renderer but wasn't sure of it. I can now proceed to creating the On$$anonymous$$ouseDown fonction :P

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

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

2D project - player respawn delay problem 0 Answers

An OS design issue: File types associated with their appropriate programs 1 Answer

Do I need a helper class? 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