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 /
This post has been wikified, any user with enough reputation can edit it.
avatar image
0
Question by VitorValadares · Aug 11, 2012 at 06:40 PM · c#collideraccess

Can't access a variable from another script through collider (C#)

Hello!

I'm having a problem while trying to access a variable that I have declared in another script. The problem is, that I'm need to check this variable from the object that I hit, BUT unity is telling me that this variable doesn't exist (I was thinking to look this variable through the collider, but it isn't working).

Here's my class that is attached on the object that will be hit:

public class CapsuleLoad : MonoBehaviour {

 public int bulletsNo;

 void Start () {
      if (bulletsNo == 0)
         bulletsNo = 1;
 }

} And this is the code that I was thinking to use:

    void OnTriggerEnter (Collider colliderObject){

     if (colliderObject.GetComponent("CapsuleLoad")){
         int currentBullets = colliderObject.GetComponent("CapsuleLoad").bulletsNo;
         Destroy(colliderObject.gameObject);
     }    
 

The exact error that I'm getting is: Type 'UnityEngine.Component' does not contain a definition for 'bulletsNo' and no extension method 'bulletsNo' of type 'UnityEngine.Component' could be found (are you missing a using directive or an assembly reference?);

(Yes, I'm using C#)

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
1
Best Answer

Answer by Seth-Bergman · Aug 11, 2012 at 07:04 PM

Just get rid of the quotes

   if (colliderObject.gameObject.GetComponent(CapsuleLoad)){
      int currentBullets = colliderObject.gameObject.GetComponent(CapsuleLoad).bulletsNo;
      Destroy(colliderObject.gameObject);
      }

If you use quotes, GetComponent will return a type of "Object", which then needs to be typecast. without the quotes, you get the actual type, in this case CapsuleLoad.

Comment
Add comment · Show 6 · 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 VitorValadares · Aug 11, 2012 at 07:31 PM 0
Share

Now I'm receiving other problems:

Expression denotes a 'type', where a 'variable', 'value' or 'method group' was expected

The best overloaded method match for 'UnityEngine.Component.GetComponent(System.Type)' has some invalid arguments

Argument `#1' cannot convert 'object' expression to type 'System.Type'

(It indicates those errors in both lines where I've removed the quotes)

avatar image Seth-Bergman · Aug 11, 2012 at 07:44 PM 0
Share

oops, change it to:

if (colliderObject.gameObject.GetComponent("CapsuleLoad")){

etc...

I'll edit it above

avatar image VitorValadares · Aug 11, 2012 at 09:27 PM 0
Share

It does work to enter on the "if" script, but, on the other hand, the 'int' part don't work.

The error remains the same: Type 'UnityEngine.Component' does not contain a definition for 'bulletsNo' and no extension method 'bulletsNo' of type 'UnityEngine.Component' could be found (are you missing a using directive or an assembly reference?);

avatar image Seth-Bergman · Aug 11, 2012 at 09:38 PM 0
Share

you may need to split that up:

 void OnTriggerEnter (Collider colliderObject){
  
 CapsuleLoad cl = colliderObject.gameObject.GetComponent(CapsuleLoad);
 
  if (cl){
  int currentBullets = cl.bulletsNo;// note below
  Destroy(colliderObject.gameObject);
  }}

BUT, if you declare the int currentBullets inside that if statement, it only exists there, and you never used it..

I assume there is more to this.. are you trying to add bulletsNo number of bullets to an existing int?

then you would just say

 totalBullets += cl.bulletsNo;

or something...

avatar image VitorValadares · Aug 11, 2012 at 09:45 PM 0
Share

Would you believe if I told you that I just did that on my code hehe. But anyway, it worked!

I just need to explicitly tell the type of the instantiated object, and add the quote:

CapsuleLoad cp = colliderObject.gameObject.GetComponent("CapsuleLoad") as CapsuleLoad;

Thanks ;)

Show more comments

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

10 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

Related Questions

Access a C# script on collison 1 Answer

How to access children's colliders. 4 Answers

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Only use specific collider 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