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 cloud4197 · Mar 30, 2015 at 03:44 PM · referencing

Having trouble referencing a script

Can anyone tell me why I get an error from the following code? I'm trying to reference a C# script (from another C# script in the same project) called HazardSpawner. I'm trying to de-activate it...

 HazardSpawner hazardSpawner;
 
 
     void Awake ()
      {
         hazardSpawner = GetComponent <HazardSpawner> ();
         hazardSpawner.enabled = false;
      }


EDIT: here is the resulting error:

NullReferenceException: Object reference not set to an instance of an object PlayerHealth.Awake () (at Assets/Scripts/PlayerHealth.cs:28)

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 meat5000 ♦ · Mar 30, 2015 at 03:58 PM 0
Share

And said error is...?

avatar image sumeeton · Mar 30, 2015 at 04:00 PM 0
Share

What error are you receiving?

avatar image UNZoOM · Mar 30, 2015 at 04:21 PM 0
Share

Whats the code at line 28 of PlayerHealth.cs ? Also , moving hazardSpawner.enabled = false; outside of awake might work.

2 Replies

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

Answer by sumeeton · Mar 30, 2015 at 05:02 PM

They are on the seperate objects. Use the following:

 GameObject.Find("HazardSpawner").GetComponent<HazardSpawner>().enabled = false;


The script you are using is searching the script component on the same gameobject.

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 cloud4197 · Mar 30, 2015 at 05:06 PM 0
Share

Blimey I'm stupid. I never realised I hadn't specified the script in the inspector. Thanks

avatar image
0

Answer by cloud4197 · Mar 30, 2015 at 04:02 PM

Sorry. Should've added that to the post.

Here it is:

NullReferenceException: Object reference not set to an instance of an object PlayerHealth.Awake () (at Assets/Scripts/PlayerHealth.cs:28)

Comment
Add comment · Show 6 · 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 sumeeton · Mar 30, 2015 at 04:07 PM 0
Share

Try moving the script to Start()

avatar image cloud4197 · Mar 30, 2015 at 04:14 PM 0
Share

No good I'm afraid.

The weird thing is I'm using the same method to reference another script and that's working fine:

 Player$$anonymous$$ovement player$$anonymous$$ovement;
 
 
     void Awake ()
     {
         player$$anonymous$$ovement = GetComponent <Player$$anonymous$$ovement> ();
     }
 
     void Death ()
     {
         player$$anonymous$$ovement.enabled = false;
     }
avatar image sumeeton · Mar 30, 2015 at 04:17 PM 0
Share

Is the HazardSpawner script attached to the game object? If it is a prefab, then check if the prefab has the HazardSpawner script attached to it.

avatar image cloud4197 · Mar 30, 2015 at 04:41 PM 0
Share

It's attached to an Empty Object within the hierarchy. The object has a transform and the script attached and nothing more. It's job is to instantiate random objects falling from the sky in random places at regular intervals.

avatar image sumeeton · Mar 30, 2015 at 04:47 PM 0
Share

Try moving the PlayerHealth script below the HazardSpawner script in the inspector. Also can you share the screenshot of the inspector when this empty object is selected?

Show more comments

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 do I hold an object within one of my spheres, to where I can click the sphere and activate the object? 0 Answers

Optimising references to other scripts 1 Answer

System.Numerics.BigInteger in Unity 5.6 1 Answer

Giving a ScriptableObject asset reference to all instances of a MonoBehavior? 0 Answers

Toggling Component's Function on a gameObject 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