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 CG-DJ · Oct 08, 2013 at 01:00 AM · errortriggerontriggerenter

Error in..."calling"(?) script

This is what happens when you don't code for a month. You forget really important things. So, I need to access this script from another script, but it's erroring out when the object with the below script hits an enemy object.

 Null Reference Exception: Object Reference not set to an instance of an object EnemyDetectionScript.OnTriggerEnter (UnityEngine.Collider hit) (at.....line 12)

What is going on? I feel like I should know this... Thanks for helping on this question.

 var playerScript : MainCharBehaviorCC;
 
 function Start()
 {
     playerScript = GameObject.FindGameObjectWithTag("Player").GetComponent(MainCharBehaviorCC);
 }
 
 function OnTriggerEnter(hit : Collider)
 {
     if(hit.collider.tag == "enemy")
     {
         playerScript.test = true;
     }
 }
Comment
Add comment · Show 4
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 ♦ · Oct 08, 2013 at 01:06 AM 0
Share

The player tagged and script present on him? And the enemy tag is correct? Collider set to 'Is Trigger'?

Perhaps try hit.collider.gameObject.tag if nothing else works.

avatar image CG-DJ · Oct 08, 2013 at 01:47 AM 0
Share

(meat, you always seem so helpful :)

Player tagged and the script is attached. Yes, the collider is set to isTrigger. If I take out line 12 and put a Debug there, no error occurs. It has to be something with referencing the script!

avatar image meat5000 ♦ · Oct 08, 2013 at 02:01 AM 0
Share

(Thanks :))

You could access test directly with GetComponent. I guess, check test has accessible scope. It must be declared outside functions and not be nested.

I suppose FindWithTag may work better. Or try CompareTag. But I don't think FindGameObjectWithTag is actually in the docs. It actually has an 's' in it 'Objects' and returns an array.

avatar image TrickyHandz · Oct 08, 2013 at 02:30 AM 0
Share

I have to agree with meat5000 here. The problem is most likely the call to GameObject.FindGameObjectWithTag(). I can't find any documentation on it. I always use GameObject.FindWithTag() in situations like this.

0 Replies

· Add your reply
  • Sort: 

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

17 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

Related Questions

Where is this (-1) coming from in my change level script? 1 Answer

C# - Problem with trigger that won't activate 1 Answer

OnTriggerStay and OnTriggerEnter won't react when the player enters them. 1 Answer

Collecting 2 items at a time by mistake. 0 Answers

OnTriggerEnter questions 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