• 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
1
Question by blender51 · Sep 20, 2012 at 12:36 AM · getcomponent

'GetComponent' is not a member of 'Object'... who said I was using an Object??

I clearly defined a variable as a GameObject, yet I cannot use GetComponent on it. I can't figure out what's wrong with my code. I need to be able to resize the arrays, so built-in arrays won't help. Any help would be greatly appreciated :D

#pragma strict

var InDialogue = 0; var InBattle = 0;

// Battle vars var battleArray : Array; var ent1 : GameObject; // << The actual GameObject. Clearly defined as GameObject. var ent1e : Entity; // Entity script that contains all movement and battle variables var en = 0; var fr = 0; var NumActiveBt = 0;

function Start () { battleArray = new Array(); }

function InitBattle() { // Initial splash screen? // Find out who's in this battle... battleArray.Push(gameObject.FindGameObjectsWithTag("Player")); var npclist : GameObject[] = GameObject.FindGameObjectsWithTag("NPC"); for (var npc1 : GameObject in npclist) { if ((npc1.GetComponent(Entity).HP > 0) && ((npc1.GetComponent(Entity).Si > 0))) { battleArray.Push(npc1); //NumActiveBt += 1; } } InBattle = 1; }

function Update () { if (InBattle == 1) { en = 0; fr = 0; //NumActiveBt = bt.length; //InBattle = 0; NumActiveBt = 100; for (ent1 in battleArray) { ent1e = ent1.GetComponent(Entity); // THIS IS THE LINE THAT RETURNS AN ERROR NumActiveBt = 200; if (ent1e.HP > 0) { //NumActiveBt++; if (ent1e.Si == 1) { fr ++; } if (ent1e.Si == 2) { en ++; } } } NumActiveBt = 300; if (fr == 0) { // LOSE GAME } if (en == 0) { // WON BATTLE // Loot screen? InBattle = 0; } } }

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

Answer by aldonaletto · Sep 20, 2012 at 12:55 AM

This error should not happen, since ent1 is a member variable declared as GameObject. The only thing I can think is that the compiler is creating a temporary variable ent1 in the for..in loop - since it's not typed, the compiler assumes the generic object type. You could try the following:

     for (var ent1: GameObject in battleArray)

Comment
Add comment · Show 4 · 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 blender51 · Sep 20, 2012 at 01:03 AM 0
Share

Thanks! I gave it a try, and changed to an ArrayList ins$$anonymous$$d. Here's my code now. It still doesn't work, and the program immediately stops once it hits the for loop.

I'm going off this example: http://forum.unity3d.com/threads/46032-Array-of-GameObjects-solved

And my code looks almost exactly like his "solved" code, yet it doesn't work for me.

#pragma strict

var InDialogue = 0; var InBattle = 0;

// Battle vars var battleArray : ArrayList; var en = 0; var fr = 0; var NumActiveBt = 0;

function Start () { battleArray = new ArrayList(); }

function InitBattle() { // Find out who's in this battle... battleArray.Add(gameObject.FindGameObjectsWithTag("Player")); var npclist : GameObject[] = GameObject.FindGameObjectsWithTag("NPC"); for (var npc1 : GameObject in npclist) { if ((npc1.GetComponent(Entity).HP > 0) && ((npc1.GetComponent(Entity).Si > 0))) { battleArray.Add(npc1); } } InBattle = 1; }

function Update () { if (InBattle == 1) { NumActiveBt = 100; for (var ent1 : GameObject in battleArray) { // This is the for loop that doesn't work // It never executes the line below. // Ins$$anonymous$$d, it changes NumActiveBt to 100 NumActiveBt = 200; } } }

avatar image aldonaletto · Sep 20, 2012 at 01:11 AM 0
Share

Check the battleArray.Count property: if it's zero, the instruction inside the for will never be executed

avatar image blender51 · Sep 20, 2012 at 01:20 AM 0
Share

The count = 2. I used the debug thingie, and stepped thru the function, and it got stuck on the "for" line.

avatar image blender51 · Sep 20, 2012 at 01:05 PM 0
Share

I solved it! :D The problem came from when I added the player to the battleArray. Thanks for the help! Here are the changes I had to make:

function Start () { battleArray = new ArrayList(); playa = gameObject.FindGameObjectWithTag("Player"); }

function InitBattle() { // Find out who's in this battle... //battleArray.Add(gameObject.FindGameObjectsWithTag("Player")); battleArray.Add(playa.gameObject); var npcList : GameObject[] = gameObject.FindGameObjectsWithTag("NPC"); for (var npc1 : GameObject in npcList) { if ((npc1.GetComponent(Entity).HP > 0) && ((npc1.GetComponent(Entity).Si > 0))) { battleArray.Add(npc1.gameObject); } } InBattle = 1; }

avatar image
0

Answer by Mortoc · Sep 20, 2012 at 12:57 AM

Your iteration variable has the same name as your member variable. Change the iterator (ent1) variable to a different name.

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

11 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 avatar image

Related Questions

GetComponent not working C# 1 Answer

A simple CS0309 problem... 2 Answers

Using raycast - Why can't I get a variable on the object I hit? 1 Answer

How to access a variable for a Component of a different GameObject ? 1 Answer

I am trying to use a variable in the parameter of gameObject.GetComponent("var's Name"). 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