Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 Kisielowsky · Apr 21, 2014 at 12:27 PM · positiongetcomponentpowerup

GIVING variables to another gameobject

HAI! I have two gameobjects, for example, powerup and player. Player and powerup have the same variables, for example:

 var health : float;
 var shield : bool;
 var ammo : int;

I'm looking for a script, that, when player x,z position (y does not matter, 2d game) is the same as powerup x,z position, copies variables values FROM powerup, and gives it TO player. The script should be attached to powerup. I thought about something like reverse GetComponent (GiveComponent?).

Waiting for your reply, Kisiel

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

Answer by xortrox · Apr 21, 2014 at 02:09 PM

This would require the "OtherObject" having a "OtherComponent" script attached that also has a variable named shared that is public.

One example:

 public class ObjectComponent : MonoBehaviour
 {
 
     public int shared = 10;
 
     void Awake()
     {
 
         GameObject.Find("OtherObject").GetComponent<OtherComponent>().shared = this.shared;
 
     }
 
 }
Comment
Add comment · Show 8 · 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 xortrox · Apr 21, 2014 at 03:52 PM 1
Share

That is correct, in javascript you just do

 function Awake()

and

 public var shared : int = 10;

I think.

avatar image xortrox · Apr 21, 2014 at 04:24 PM 1
Share

You need to specify which component to get from Cube

 GetComponent<SomeComponentType>()
avatar image xortrox · Apr 21, 2014 at 04:26 PM 1
Share
 public class CubeComponent : $$anonymous$$onoBehaviour
 {
 
 public bool allowleft = false;
 
 }
avatar image xortrox · Apr 21, 2014 at 04:49 PM 1
Share

no quotation marks and no file extension just skrypt

avatar image Kisielowsky · Apr 21, 2014 at 05:29 PM 1
Share

Done, thank you very much!

Show more comments
avatar image
0

Answer by $$anonymous$$ · Apr 21, 2014 at 04:08 PM

you could put:

var sharedvalue : int;

function Update() { sharedvalue = otherscriptname.varname; }

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.

Follow this Question

Answers Answers and Comments

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

Related Questions

Can't get enemy to chase player 2 Answers

How do I instantiate at a certain point? 2 Answers

Camera rotation around player while following. 6 Answers

SMB Power Up Animation - Player Position Issue 1 Answer

Design Advice: Power Up System (static variables?) 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