• 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 Danisuper · Oct 18, 2013 at 08:07 PM · communicationpower-up

communication with other gameobjects for powerup

hi everybody,i have a problem.I am creating a powerup that when you reach the score 30 the powerup is actived and it goes to change the movement. but the powerup script and the script of the movement are'nt in the same gameobject. I tried with this code but doesnt work:

 movimento move;
 scorePlayer scrPl;
 public int scorePower;
 
 void Start () {
     move=GetComponent<movimento>();
     scrPl = GetComponent<scorePlayer>();
     move = GetComponent<movimento>();// class movement
     scorePower = scrPl.score;
 }

 // Update is called once per frame
 void Update()
 {
     if (scrPl.score >= 20)
     {
         move.MOVE();
     }
         

and this is the class movement:

 powerUp pwrp;
 public int scoreInt;
 public bool MoveIt = false;

 // Use this for initialization
 void Start()
 {
 pwrp = GetComponent<powerUp>();

 }

 public void MOVE()
 {
     if (pwrp.scorePower >= 20)
     {
         MoveIt = true;
     }
 }

 void Update()
 {
     if (MoveIt)
     {
         if (Input.GetKey(KeyCode.LeftArrow))
         {
             transform.position = new Vector3(-30 * Time.deltaTime, 0, 0);
         }
         if (Input.GetKey(KeyCode.RightArrow))
         {
             transform.position += new Vector3(30 * Time.deltaTime, 0, 0);
         }

     }
 }
 

what went wrong??? thanks in advance :-)

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

Answer by zombience · Oct 18, 2013 at 08:13 PM

Hmm, I'm not sure how you're linking the scripts together from looking at your code, but you can try using the Advanced CSharp Messenger scripts available over at the unifycommunity wiki:

http://wiki.unity3d.com/index.php/Advanced_CSharp_Messenger

I use it often in projects. It's an extremely handy way to help scripts interact that have no direct references to each other.

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 Danisuper · Oct 20, 2013 at 08:40 AM 0
Share

Thanks but gow can i do that using Sendmessage?

avatar image zombience · Oct 21, 2013 at 09:49 PM 0
Share

You would use this ins$$anonymous$$d of using Send$$anonymous$$essage. So, you would have two scripts:

 public class Receiver : $$anonymous$$onoBehaviour
 {
     void Start()
     {
         $$anonymous$$essenger.AddListener("PowerUp", DoPowerUpStuff);
     }
 
     private void DoPowerUpStuff()
     {
         Debug.Log("powering up, aww yea");      
     }
 }
 
 public class Sender : $$anonymous$$onoBehaviour
 {
     public bool send$$anonymous$$essage = false;
     void Update()
     {
         if(send$$anonymous$$essage)
         {
             $$anonymous$$essenger.Broadcast("PowerUp");
         }
     }
 }


the reason this is superior, is because when doing "Send$$anonymous$$essage", unity has to do a search on the object to see if any of the components attached are capable of responding to the Send$$anonymous$$essage. The search can be a performance drain.

using this method, with registration for messages right at the beginning, there is a link already established between sender and listener, and the command can happen without any searching.

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

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

Related Questions

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

Communication between objects and other scripts, variables and properties 1 Answer

Help! How can I get my power up script to work? 1 Answer

Can someone help me fix my Javascript for Flickering Light? 6 Answers

Material doesn't have a color property '_Color' 4 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