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 darkironphoenix · May 04, 2012 at 04:03 PM · gameobjectsendmessage

Sending values to other gameobjects

Ok, first off, i am still a bit of nub when it comes to code, so bare with me :)

I am able to send values to scripts in the same gameobject, but for some reason i cannot find a way to send values to a script on another gameobject.

I have a gameobject called book. When the player picks up the book, the book is deleted and a skill point would be added to the skill points manager as part of the character stats. The book and the code for picking up the book is on one gameobject, and the skills stuff is on the Main Camera.

My last experiment was using sendmessage, and here is the code for it. If there is something else i should be using, throw it at me :)

Skills_pane

 var spoint : int = 0;

 function skillpoint(spoint : int){
 
 spoint += 1;

 }

skillbook

  var dist : int;
 var player : Transform;
 var book : Transform;
 
 function Start () {
 
 }
 
 function Update () {
 
 dist = Vector3.Distance(player.transform.position,book.transform.position);
 
 if (dist == 1) {
     if (Input.GetKeyUp("e")) {
 gameObject.SendMessage ("skillpoint");
 DestroyObject (gameObject);
 
 }
 }
 }

Anything anyone can suggest will be a lifesaver.

Comment
Add comment · Show 1
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 hijinxbassist · May 04, 2012 at 04:56 PM 0
Share

@darkironphoenix No reason to send a message. You can say something like this on the book.

 Camera.main.GetComponent(Skills_pane).skillpoint(somePoints);

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by stingman · May 04, 2012 at 04:56 PM

First off, Send message will only send that message to anything on your current game object. Broadcast message will send it to the parent and all of its children. However since you are dealing with 2 seperate game objects I would call on the skillpoint function directly from your other game object. (I also changed your dist line because you already declared those variables as Transforms. There is no need to write player.transform.position... just say player.position. Just an FYI :) Here is a modification of your script. Try messing with this... it may not be exactly what you want... but this is my idea of where to go. :

 var cam : Camera; // drop your camera that has the skills script into this variable
 var dist : int;
 var player : Transform;
 var book : Transform;
 
 function Update () {
 
 dist = Vector3.Distance(player.position, book.position);
 
 if (dist == 1) {
     if (Input.GetKeyUp("e")) {
 cam.GetComponent(Skills_pane).skillpoint();
 Destroy(gameObject);
 
 }
 }
 }
Comment
Add comment · Show 1 · 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 darkironphoenix · May 04, 2012 at 06:27 PM 0
Share

Thanks for the help.

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

6 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

SendMessage has no reciever 0 Answers

get all scripts attached to gameobject 2 Answers

C# Scripts don't attach to game objects, what happened? 2 Answers

What is wrong with this script? Error connecting with another objects scripts/functions 1 Answer

Send Message not working! 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