Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 TheIBPoliceman · Aug 25, 2011 at 10:56 PM · arrayssendmessagereferencing

SendMessage to objects in an array

Hey,

So I'm trying to make a fighter squadron-like system for my game, where there is a general squadron manager object and several non-parented child fighter gameobjects. As I understand it this is how a lot of games do it.

Anyways, I'm stuck on creating a system to send orders to the units in the squadron from the manager object. Right now, when the manager is created, it instantiates the fighters at certain points on the mothership, and then adds them to an array. So far so good.

 function LaunchAllFighters(interval:float) {
     var tempMember:GameObject;
     
     for (var n=0; n < (numCraft ); n++) {
         var launchPt = hangarBones[hangarIndex];
         if (hangarIndex < maxIndex) {
             hangarIndex = hangarIndex+1;
         } else {
             hangarIndex = 0;
         }
         
         tempMember =Instantiate (fighter, launchPt.position,launchPt.rotation);
         
         tempMember.rigidbody.velocity = tempMember.transform.forward*launchVel;
         tempMember.SendMessage("OnJustLaunched",launchTime);
         tempMember.SendMessage("AssignSquadController",transform.GetComponent(SquadronController));
         
         
         squadMembers.Add(fighter);
         yield WaitForSeconds(interval);
     }
     launchComplete = true;
 }

The problem comes when I try to use SendMessage to send orders to the objects in the array. Here I'm just passing a single element of a Vector3 array (the unit's position in formation) to the objects.

 function FormUpSquadron() {
     var n = 0;
     for (var ship: GameObject in squadMembers) {
         ship.SendMessage("Teleport",squadPosns[n]);
         n = n+1;
     }    
 }

The objects in the squadron have this function in their attached script:

 function Teleport(pos:Vector3) {
     transform.position = pos;
 }
 

I get a "SendMessage has no reciever" error. Usually that happens when the object you're sending to has no corresponding function, but this one does. So I think it must be something to do with how the object is referenced in the array. I feel like it's something really simple and embarrasing too.

Thanks.

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

Answer by aldonaletto · Aug 26, 2011 at 03:32 AM

You're adding the prefab fighter to squadMembers, but you should add tempMember:

    squadMembers.Add(tempMember);
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 TheIBPoliceman · Aug 26, 2011 at 03:37 PM 0
Share

Thanks! So it was a silly embarrasing mistake!

avatar image aldonaletto · Aug 26, 2011 at 06:02 PM 0
Share

It happens to everyone! But the rest of your code shows that you know what you're doing, so it was clearly a fool mistake.

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

SendMessage - how to send 2 details? - 2 Vector3's actually 1 Answer

Error referencing custom class elements 0 Answers

Send a message to multiple scripts within an Array - Javascript 1 Answer

SOLVED: Problem with SendMessage -- silently failing 2 Answers

Communicating between objects without any physical connection? 3 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