• 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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

No one has followed this question yet.

Related Questions

Change and Access another scripts Variables 1 Answer

How to access Planes of Particle System by script 1 Answer

How to access gameObject variable script 2 Answers

Cannot access a variable in another Script. 2 Answers

How do I access information (eg, variables, functions, properties) on other objects from inside a script? 2 Answers

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges