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 dtrip · Dec 21, 2021 at 08:05 AM · enemy damage

Calling the other object's script does not work.

This is the Bulllet's code. Upon collision with an Alien, it calls the alien's GetShot() method. All compiles and runs Ok, except the alien's GetShot() method does not get called (it has a Debug.Log inside, but does not print anything).

 void OnCollisionEnter(Collision collision)
 {

     //Debug.Log("Collision");
     

     if (collision.gameObject.name == "fly1(Clone)")  //This is the alien's name 
     {
         //This works
         GenericAlien ga = collision.gameObject.GetComponent<GenericAlien>();
        //This too
         Debug.Log("Calling fly to get shot");
        **//This does not work (it has a Debug.Log inside but does not print it). No errors.** 
         ga.GetShot();
         //This works
         Debug.Log("Destroying myself");
         //This too, bullet gets destroyed
         Destroy(gameObject);
     }

     //Check for a match with the specific tag on any GameObject that collides with your GameObject
     if (collision.gameObject.tag == "MyGameObjectTag")
     {
         //If the GameObject has the same tag as specified, output this message in the console
         Debug.Log("Do something else here");
     }
 }
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 EliasMiettinen · Dec 21, 2021 at 12:08 PM 0
Share

Are your GetShot method static?

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by endasil_unity · Dec 21, 2021 at 11:55 AM

It is unclear if you mean that Calling fly to get shot or a debug.log in ga.GetShot is what you talk about when you say it does not print. Could help if you show the ga.GetShot code.

If Debug.Log("Calling fly to get shot"); is dispayed, it should call ga.GetShot(); nothing wrong with the code here.

Are there any if statments or something that could prevent your debug.log from printing in the other code?

Have you looked in the inspector to see that your instantiated object has the correct code?

Have you tried using the Debugger?

Attaching the Unity Debugger to your project from Visual Studio, set a breakpoint on the ga.GetShot row. Press play. When visual studio stops your game and highlights the breakpoint, press f11 to step into it and see what happens.

If you're not familiar with the visual studio debugger and unity, please google it. It is something that is very helpful when you debug this type of code and can save you lots of frustrating debugging hours.

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 dtrip · Dec 25, 2021 at 08:25 AM

Hello ! Thanks for the reply and the debugging advise. I got it to work.

The Generic Alien prefab was an ABSTRACT class, something for all Aliens to inherit. Once I turned into a : Monobehavior it worked. Don't ask my why, I have no idea ha ha.

So I am done with inheritance I guess. I 'll just put all "common Alien stuff" (like hit points, shooting style, etc) in ANOTHER script, and have all Aliens run that script too. No need to have only one script per game object, right ? I don't know why I had enforced myself to think "one script per game object".

Thanks, your reply was very useful. Dimitris

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

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

133 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

Related Questions

Why is my collider not sending damage? 3 Answers

Bullet Interaction 2 Answers

why isn't my enemy shooting at me 1 Answer

head shots 1 Answer

how to kill an emeny? 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