• 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 /
  • Help Room /
avatar image
Question by bornacvitanic · Jul 28, 2019 at 09:34 PM · errorgameobjecterror messagebug-perhapscollision2d

Unity bug gives false error messages or doesen't work without a Debug.Log() line present

So I wanted to make a simple projectile damage on collision script. But since the projectile is a prefab, i cannot drag and assign my player as the target. Thats why i wanted to leave the player gameobject empty and assign it via script. But even though it works, it gives me an error that the player isn't assigned every time the bullet spawn:

private Health health; public GameObject player;

 void Start()
 {
     if (player == null){
         player=GameObject.FindGameObjectWithTag("Player");
     }
     health=GameObject.FindGameObjectWithTag("Player").GetComponent<Health>();
 }

 void OnCollisionEnter2D(Collision2D col){
     if(col.gameObject.tag==player.tag){
         health.DecreaseHealth();
     }
     Destroy(gameObject);
 }

So me not wanting to have an error message, I removed the player gameobject:

private Health health;

 void Start()
 {
     health=GameObject.FindGameObjectWithTag("Player").GetComponent<Health>();
 }

 void OnCollisionEnter2D(Collision2D col){
     Debug.Log(player.tag);
     if(col.gameObject.tag==GameObject.FindGameObjectWithTag("Player").tag){
         health.DecreaseHealth();
     }
     Destroy(gameObject);
 }

and when I do this, it doesen't even create the bullet, which I do in a totally different script.It will only work like this if i put the Debug.Log command there with the player.tag as argument.

Are both of these bugs or am I doing something wrong?,So I was using "public GameObject player" to assign my player, and but when I do, it constantly spams error messages that it isn't assigned, which it isn't in the inspector, but it is in the script in start.

Comment

People who like this

0 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 xxmariofer · Jul 28, 2019 at 09:48 PM 0
Share

please share the errors and the lines where they appear. this cant give you an error

 if (player == null){
      player=GameObject.FindGameObjectWithTag("Player");
  }

and adding/removing the debug.log is for sure not the issue. what do you mean that works? that doesnt gives error messages? how is the debug.log player.tag even compile if you removed the player var?

1 Reply

· Add your reply
  • Sort: 
avatar image

Answer by AAcat · Jul 28, 2019 at 09:47 PM

I don't know if this helps, but try changing "void start" to "void awake". This usually helps with initialization and errors of the likes. ( Also the error code and line helps to know too :v )

Comment
bornacvitanic

People who like this

1 Show 2 · 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 bornacvitanic · Jul 28, 2019 at 10:02 PM 0
Share

tried it, didn't help.

avatar image xxmariofer bornacvitanic · Jul 28, 2019 at 10:10 PM 0
Share

as i said in my comment is imposible that these line gives an error

 if (player == null){
      player=GameObject.FindGameObjectWithTag("Player");
  }

the debug.log is not getting affected, is the player spawned / destroyed by code? the debug.log cant be causing the issue and most likely is not a unity bug

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

257 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 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 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 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 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 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 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 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 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 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 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 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

Can't add Orbital script - "The script needs to derive from MonoBehaviour!" 0 Answers

BCE0019: 'gameObject' is not a member of 'Object'. 1 Answer

Animator not initialized "cautions" 0 Answers

An instance of type 'UnityEngine.Behaviour' is required to access non static member 'enabled'. 0 Answers

Problem while downloading Creator Kit- Beginner code 0 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