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
-1
Question by Benjames · Oct 17, 2013 at 01:04 PM · weaponpointersearchstructeliminate

How can I eliminate searching for a struct corresponding to the collider returned by OnCollisionEnter()

I have a player struct with a gameobject, transform, necessary values, and a weapon. I want to call OnCollisionEnter for my weapon from my player.

I am trying to think out how to detect a collision. I want to get the values from the player, the weapon, and the effected enemy without running a function that searches through names or something to find a match.

I'm not sure how abstract this is, feel free to ask questions

Comment
Add comment · Show 6
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 Benjames · Oct 17, 2013 at 01:08 PM 0
Share

$$anonymous$$aybe I could have a pointer on my weapon object that points back to the struct of the player. Call OnCollisionEnter() but then I would still be searching through names to find the struct related to the effected enemy.

avatar image Turkeybag · Oct 17, 2013 at 01:20 PM 0
Share

Are you trying to call a function on your gun when the player hits something or what is it exactly that you're trying to do gameplay wise? $$anonymous$$ight be easier to explain what outcome your looking for rather than how you're trying to get that outcome.

avatar image Benjames · Oct 17, 2013 at 01:20 PM 0
Share

Is it possible to store a pointer in something that gets returned by OnCollisionEnter?

avatar image Turkeybag · Oct 17, 2013 at 01:35 PM 0
Share

I'm not entirely sure what you mean by pointer but you can store pretty much anything and send it pretty much anywhere. I'm still unsure of what you're actually trying to do so I can't really help much. Do you want to send the collision information from when the player is hit by something to the weapon? Are you trying to call a function in the other when one is hit? Do you want to edit the variables in one with the other? Be specific and I can help :P

avatar image Jessy · Oct 17, 2013 at 02:17 PM 0
Share
  • for this being asked too much already, you not showing that you've learned from that knowledge, and for weird tags.

Show more comments

3 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by Jessy · Oct 17, 2013 at 02:18 PM

You should learn how to use EventArgs. Fire an event in OnCollisionEnter, which contains the data you need. Subscribe to the event where necessary, and use the data as you see fit.

Comment
Add comment · 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 Benjames · Oct 17, 2013 at 11:57 PM 0
Share

Sup! so I thought about it.

So I call OnCollisionEnter() in my weapon. I instantiate my weapon in my player and set a pointer"Owner" in my weapon to the player struct.

Right..

enemy's each have a script with another pointer that is set to their corresponding struct.

so on collision with enemy the weapon uses its "Owner" pointer to get info like damage and applies that to the enemy's health that we retrieve through collision.collider.GetComponent"script"().pointer.health;

its kinda like a web I guess

Although I answered this all too oftenly asked question myself, I will learn EventArgs.

Leroy Jenkins!!!

avatar image meat5000 ♦ · Oct 18, 2013 at 12:14 AM 0
Share

Yup, you can use GetComponent to call any function including OnCollisionEnter from another script.

avatar image
0

Answer by rutter · Oct 18, 2013 at 12:26 AM

It's certainly possible to pass that collision event to another script -- use GetComponent to find the other script, write a function which takes the data in, then call that function from the original object's OnCollisionEnter.

For example, I once worked on a game where the player had multiple colliders: incoming projectiles could hit their shield, or their body. I had a simple component attached to each child collider, to detect the collisions, which could then pass them up to the main player script for processing.

Here's another UA thread which discusses messages and calls between scripts.

I would strongly recommend against generating your own collision data. Much easier to delegate.

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 Benjames · Oct 19, 2013 at 04:31 PM

So I couldn't change some variables in my structs from like a {get;set;} pointer like thing.. ya know that thing. Anyways I went looking around and found someone with that same problem. The solution was to change the structs into classes.

I changed over and painfully struggled to get it working until I found out that I had to set each class variable in an array to a new class object. Like I have two legs right.

pl.leg=new limb[2]; //Right?

so I had to go through each one and declare it again

pl.leg[0]=new limb();

pl.leg[1]=new limb();

I guess it was simple enough, with larger arrays its easy to just use a FOR statement. I had to do extra checks to make sure thing were initialized to like if(Owner!=null) aswell.

Anyway I got it all going, thanks meat, rutter, jessy, turkeybag!

alt text

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

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

array is pointer and float variable is not - why not? C# 1 Answer

Storage of types in Structs and Classes 1 Answer

Can I call OnCollisionEnter() for an object from another object? 0 Answers

Enemy Seeking Missiles 0 Answers

NativeList.GetUnsafePtr() throwing The type 'T' must be a non-nullable value type 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