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 henrypuspurs · Jun 23, 2013 at 05:55 PM · c#guiif

Referencing a script on the same object comes back with an error

I am getting "NullReferenceException: Object reference not set to an instance of an object" even though the script I am referencing is on the same Object, strangely I got another script on another object to work, I used the same code and modified it for a different use, one I thought should be simpler since both scripts are on the same Object.

This is what doesnt work

 public class CrosshairGUI : MonoBehaviour
 {
     
     private CrosshairRay rayscript;
     
     private Color curcol;
     
     void Awake() 
     {
     rayscript = GetComponent<CrosshairRay>();
     }
     
     void OnGUI()
 
         {
         GUI.color = curcol;
         GUI.Label (new Rect(Screen.width/2,Screen.height/2, 50, 50), "+");
         }
     
     void Update()
     {
         if(rayscript.imnear)
         {
             curcol = Color.red;
         }
         else
         {
             curcol = Color.white;
         }
 }
 }
     

and the one that does

 public class Muteaudioclip : MonoBehaviour
 {
     public GameObject player;
     
     private CrosshairRay rayscript;
      
     void Awake()
         {
         rayscript = player.GetComponent<CrosshairRay>();
         }
              
              
             void OnMouseDown()
             {
                 if(rayscript.imnear)
                 {
              
              
                 if(audio.mute == false)
                  
                     {
                     audio.mute = true;
                     Debug.Log ("Sound Off");
                     }
                 else
                     {
                     audio.mute = false;
                     Debug.Log ("Sound On");
                     }
                 }
             }
 }

Im guessing its something simple Ive overlooked.

Comment
Add comment · Show 8
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 Eugenius · Jun 23, 2013 at 06:03 PM 0
Share

I'm not sure if this is it but you made your player GameObject public while the rayscript variable is private, try making the script variable public as well and I think that should work ...

avatar image Em3rgency · Jun 23, 2013 at 06:04 PM 0
Share

Eugenius > I doubt that's it. The public player is in the script thats working. Its the rayscript variable thats giving him the error, which is private in both cases.

avatar image Eugenius · Jun 23, 2013 at 06:08 PM 0
Share

@Em3rgency, that's what I suggested, that the rayscript var is the actual problem :).

avatar image henrypuspurs · Jun 24, 2013 at 10:47 AM 0
Share

private or public it doesnt seem to make a difference "NullReferenceException: Object reference not set to an instance of an object CrosshairGUI.Update () (at Assets/$$anonymous$$ain Project/Scripts/CrosshairGUI.cs:25)" still showing.

Im guessing the :25 means line 25 which is "if(rayscript.imnear)" but again, it works fine on the other script which is on a different object from the CrosshairRay script Im referencing.

avatar image Eugenius · Jun 24, 2013 at 11:19 AM 0
Share

@henrypuspurs could you please share the piece of code from the rayscript with the imnear variable?

Show more comments

4 Replies

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

Answer by henrypuspurs · Jul 19, 2013 at 12:10 PM

Combined GUI Crosshair and Raycast script works fine attached to the camera, in case anyone wants the solution here it is.

 using UnityEngine;
 using System.Collections;
 
 public class Crosshair : MonoBehaviour 
 {
 
 
     private Color curcol;
     public float interactionDistance = 3f;
     public bool imnear;
     
     void Start () 
     {
         Screen.lockCursor = true;
     }
     
     void OnGUI()
 
         {
             GUI.color = curcol;
             GUI.Label (new Rect(Screen.width/2,Screen.height/2, 50, 50), "+");
         }
     
     void Update()
     {
         
     
         RaycastHit hit;
          
         Debug.DrawRay(transform.position, transform.forward * interactionDistance);
          
         if(Physics.Raycast (transform.position, transform.forward, out hit, interactionDistance)) 
         {
             if(hit.collider.tag == "Clickable")
                 {
                     imnear = true;
                 }
             else
                 {
                     imnear = false;
                 }
         }
         else
             {
                 imnear =false;
             }
     
         if(imnear == true)
             {
                 curcol = Color.green;
             }
         else
             {
                 curcol = Color.white;
             }
 
     }
 }

And an example of how that script is referred to from another object.

 using UnityEngine;
 using System.Collections;
 
 public class Audiomute : MonoBehaviour
 {
     private GameObject player;
     private Crosshair looking;
      
     void Awake()
         {
         GameObject player = GameObject.Find("Player/Camera");
         looking = player.GetComponent<Crosshair>();
         }
              
              
             void OnMouseDown()
             {
                 if(looking.imnear)
                 {
              
              
                 if(audio.mute == false)
                  
                     {
                     audio.mute = true;
                     //maybe change colour of halo
                     Debug.Log ("Sound Off");
                     }
                 else
                     {
                     audio.mute = false;
                     //maybe change colour of halo
                     Debug.Log ("Sound On");
                     }
                 }
             }
 }
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 Eugenius · Jun 24, 2013 at 02:47 PM

Instead of:

 if(Physics.Raycast (transform.position, transform.forward, outhit, interactionDistance)) 
     {
     imnear = (hit.collider.tag == "Clickable");
     }

Try to do this:

 if(Physics.Raycast (transform.position, transform.forward, out hit, interactionDistance)) 
        {
 if(hit.collider.tag == "Clickable"){
        imnear = true;
 }
        }
Comment
Add comment · Show 5 · 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 henrypuspurs · Jun 24, 2013 at 03:41 PM 0
Share

Still no luck, this is getting really frustrating, it should work, but refuses to.

avatar image henrypuspurs · Jun 24, 2013 at 03:54 PM 0
Share

O$$anonymous$$ done some more testing, now the script does everything I need it to, but the error still comes up, which is a pain and may cause issues down the line.

avatar image Em3rgency · Jun 24, 2013 at 04:17 PM 0
Share

$$anonymous$$aybe add an if(rayscript != null) ? That's the last thing I can think of...

avatar image henrypuspurs · Jun 24, 2013 at 04:41 PM 0
Share

I don't quite understand, where on what script would I put that? (you might have guessed by now Im completely new to coding)

avatar image Em3rgency · Jun 24, 2013 at 04:44 PM 0
Share
      void Update()
         {
            if(rayscript != null)
            {
                if(rayscript.imnear)
                {
                  curcol = Color.red;
                }
                else
                {
                  curcol = Color.white;
                }
             }
          }
avatar image
0

Answer by SpecticalPro · Jun 24, 2013 at 06:09 PM

try this:

public class CrosshairGUI : MonoBehaviour {

 private CrosshairRay rayscript;
 
 private Color curcol;
 
 void Awake() 
 {
 rayscript = GetComponent<CrosshairRay>();
 }
 
 void OnGUI()
 
    {
    GUI.color = curcol;
    GUI.Label (new Rect(Screen.width/2,Screen.height/2, 50, 50), "+");
    }
 
 void Update()
 {
    if(rayscript!=null){
    if(rayscript.imnear)
    {
      curcol = Color.red;
    }
    else
    {
      curcol = Color.white;
    }
    }else{
       rayscript = gameObject.getComponent<CrosshairRay>();
    }


} }

Sorry for the bad tabbing

Comment
Add comment · Show 3 · 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 Em3rgency · Jun 24, 2013 at 06:13 PM 0
Share

I already suggested this down in the comments below ;)

avatar image SpecticalPro · Jun 24, 2013 at 06:25 PM 0
Share

sorry, entered it an hour ago, guess it took this long to go through

avatar image Em3rgency · Jun 24, 2013 at 06:31 PM 0
Share

Its because you're new ;)

avatar image
0

Answer by SpecticalPro · Jun 24, 2013 at 06:08 PM

try wrapping the if/ else if statement in your update with a null check for the rayscript, if its null set it to gameObject.getComponent();

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 henrypuspurs · Jul 03, 2013 at 03:14 PM 0
Share

Could you give me an example of how to do that? I hate to ask for a favour like that but I have no coding experience so pretty much everything is new to me and Im learning as I go (as I need a script, I try and learn how to do it).

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

18 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

Related Questions

Distribute terrain in zones 3 Answers

Multiple Cars not working 1 Answer

Elements in draggable window do not remain fixed [SOLVED] 2 Answers

Create Multiple Foldouts. 1 Answer

How to set high score (best time) with playerprefs 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