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 TheShinx93 · Oct 20, 2014 at 06:32 PM · c#info

read a variable from an object in the trigger

Hello everyone I and a friend are creating a mini-moba, but we have a problem in the script for towers

basically I would like that when a player or a bot comes within range of the tower, it has to read from the script "info" (awarded to the player and bot) belongs to which team the player or the bot, and in case an enemy and points to him.

I created this script helping with the documentation of unity, but I can not solve this problem:

 Assets/script/game/torretteB.cs(22,49): error CS1061: Type `UnityEngine.Component' does not contain a definition for `Team' and no extension method `Team' of type `UnityEngine.Component' could be found (are you missing a using directive or an assembly reference?)
 

this are the script:

 using UnityEngine;
 using System.Collections;
 
 public class torretteB : MonoBehaviour {
 
     public int teamID; //0 red team -- 1 Blueteam
     public int teamobject;
     private Vector3 targetpoint = Vector3.zero;
     GameObject kObject;
     // Use this for initialization
     void Start () {
         teamID=1;// blueteam
     }
 
     void OnTriggerEnter(Collider collider)
     {
         kObject=collider.gameObject;
         if(kObject.GetComponent("info").Team != teamID)
         {
             targetpoint = kObject.gameObject.transform.position;
             Quaternion rotation = Quaternion.LookRotation (targetpoint - transform.position);
             transform.rotation  = rotation;
         }
     }
 }

sorry for my english, I am italian ;)

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

Answer by mwbranna · Oct 20, 2014 at 06:40 PM

GetComponent returns an object of type "Component". The class "info" that you create is a subclass of Component, so you need to cast the returned object to "info".

Try something like this:

 info infoObj = kObject.GetComponent("info") as info;
 if(infoObj.Team != teamID)

Also, class names (info) usually start with a capital letter, and fields (Team) usually start with a lowercase letter.

Edit: Also, the normal C# way of getting components would be easier here since passing the parameterized type will automatically return an "info" instead of a "component".

 if(kObject.GetComponent<info>().Team != teamID)

Comment
Add comment · Show 3 · 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 TheShinx93 · Oct 20, 2014 at 09:38 PM 0
Share

I thank you for the help, but now gives me this error:

 NullReferenceException: Object reference not set to an instance of an object
 torretteB.OnTriggerEnter (UnityEngine.Collider collider) (at Assets/script/game/torretteB.cs:20)
 

line:

 if(kObject.GetComponent<Info>().$$anonymous$$m != $$anonymous$$mID)
avatar image KpjComp · Oct 20, 2014 at 10:02 PM 0
Share

That sound's like the object you collided with does not have the info classed attached.

avatar image TheShinx93 · Oct 20, 2014 at 11:38 PM 0
Share

ok resolved thanks all :)

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Making a bubble level (not a game but work tool) 1 Answer

An OS design issue: File types associated with their appropriate programs 1 Answer

Renderer on object disabled after level reload 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