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 birdman · Aug 02, 2010 at 06:39 PM · getcomponentontriggerenter

accessing script components from external objects

here is a simplification of the problem I'm having.

object one has a script named targetscript:

function yougotme()
{
Destroy(gameObject) ;
}

object two has a script that on collision should collect what the object hit is (one), get the script associated and call a function in it.

function OnTriggerEnter (other : Collider)
{
var activatescript = other.GetComponent(targetscript);
if (other.GetComponent(targetscript))
{
other.GetComponent(targetscript).yougotme();
}

}

The problem is that at compile time, such collision does not exist yet and the compiler can not find the yougotme() component. The error is "'yougotme' is not a member of 'UnityEngine.Component'"

Our point here is to retain the triggering of the action on the weapon (object one) and the resulting action on the receiving object (object two), so that we can have multiple weapons trigger actions that are specific to multiple targets. We are certainly open to another strategy to the same final goal.

Any help is appreciated.

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

2 Replies

· Add your reply
  • Sort: 
avatar image
2

Answer by Mike 3 · Aug 02, 2010 at 06:43 PM

You can simplify the getcomponent part to just this (which stops you getting it 3 times o.o)

var activatescript : targetscript = other.GetComponent(targetscript);
if (activatescript != null)
{
    activatescript.yougotme();
}

Assigning to a variable with the right type is the key you were missing - after that, you just use it as normal

The error is not because the collision hasn't happened yet but because you're using a strictly typed environment (either iphone, or #pragma strict on desktop). If you want to call a function on a variable, that variable has to be the right type - GetComponent is just returning a basic Component reference to you, so you need to type it manually

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
1

Answer by Ashkan_gc · Sep 01, 2010 at 06:43 AM

as Mike said. GetComponent returns a component reference and the result need to be casted to the correct type. in unity 3 you can access generic versions that return the correct type. in C# and strictly typed js (iphone or pragma_strict) you need to cast it yourself. in C# you should do something like

targetscript t;
t = (GetComponent(typeof(targetscript)) as targetscript);
//or use the generic version
t = GetComponent<targetscript>();
//and then 
t.yougotme();

try to code your names like YouGotMe() and TargetScript. it's really more readable for you and others. the js syntax is what Mike said.

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

No one has followed this question yet.

Related Questions

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

Error accessing var from other scripts within project via GetComponent 1 Answer

enemy attack not recognizing players stats 1 Answer

How to get a variable value from another script(C#)? 1 Answer

Cannot seem to get a reference to a script component to work, despite many different attempts and a lot of work 2 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