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 RSud · Nov 01, 2010 at 05:07 AM · collisionvariableaccess

Collision object and access to a script variable?

In a script that is tied to a prefab for an object I have the following:

function OnCollisionEnter(collision : Collision) { print(collision.gameObject.name); }

And this works fine, when my game is running on collision the colliding gameObject name is showing up in the status line.

I have a variety of gameObjects (instantiated prefabs) in my game and each prefab its own script.

Within each script I define 'var Alliance : int;'.

What I am having trouble with is in the OnCollisionEnter routine I want to access the Alliance variable that is in the script of the collision gameobject.

Since the script name that attaches to the gameObject varies (I name the script something different) what would be the syntax for me to access the Alliance variable?

essentially I want to do:

collisionObjectId = collision.gameObject."script name".Alliance;

but since the script name varies, what do I do here?

thanks for any help!

Comment
Add comment
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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Maltus · Nov 01, 2010 at 05:14 AM

//yourscript.js var Alliance : int; function OnCollisionEnter(collision : Collision) { print(collision.gameObject.name);

var hitAllianceint : int = collision.gameObject.GetComponent(yourScript).Alliance; print(hitAllianceint.ToString()); }

this should work

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 RSud · Nov 02, 2010 at 03:49 AM 0
Share

Thanks but this doesn't appear to answer my question.

In you example I must replace 'yourScript' with the name of the script. Yes.

$$anonymous$$y issue is that the colliding gameObject can have different script names. For example, I call the script that is attached to a enemy ship 'Enemy_AI.js'. I call the script that is attached to an enemy bullet 'Enemy_bullet.js'. Both of these will have a variable Alliance. The object whose OnCollisionEnter() is being called is the player ship which could be colliding with an enemy ship or an enemy bullet.

What am I missing here?

avatar image ThomHurks · Dec 21, 2010 at 02:51 PM 0
Share

You need to specify the function you want to call on those objects in an interface. So you have the function "int GetAlliance();" in an interface. Then your different scripts (bullet, ship) need to implement that interface. This looks like: "public class Enemy : iAttackingObject", where iAttackingObject is the name of the interface.

When you have that, you can do this:

int allianceInt = collision.gameObject.GetComponent(iAttackingObject).GetAlliance();

Is that what you want?

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

No one has followed this question yet.

Related Questions

Access variable from another script? Health! 3 Answers

Accessing a variable in one script from another script? (using C#) 1 Answer

Can I make variables visible to other scripts without making them visible in the Inspector? 1 Answer

Editing a variable from another script on collision 3 Answers

Is it possible to change a variable, into a script not assigned to any game object? 3 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