• 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
Question by ChrisHawthorn · Jan 23, 2013 at 07:41 PM · gameobjectvariablegetcomponent

How to set a variable equal to another variable in another gameobject?

What I'm trying to do is a build a system where the player can see a weapon on the ground, it clones a new weapon and carries all the set variables over to that clone (ammo etc), pick up the clone, and that should replace the other weapon they're already holding.

My other game object is called Weapon Controller, and that has the script called weaponHandler, which has all the code used to hold any weapon it's given and switch between the two of them. weaponHandler has to two variables, one for each weapon.

Any help I can get I'd appreciate :')

 #pragma strict
 
 var weapon : GameObject; //any weapon can be used here
 var currentClip : float;
 var currentAmmo : float;
 var currentWeapon : Component;
 
 function Start ()
 {
 
 }
 
 function OnTriggerEnter (other : Collider) 
 {
 
     print("Item is in range");
     var weaponClone : GameObject = Instantiate (weapon); //clone the weapon
     currentWeapon = gameObject.GetComponent(WeaponHandler); //check what weapon is in the weapon1 variable
     currentWeapon.weapon1 = weaponClone; //change that current weapon to our new cloned weapon
     //var currentSelected = currentWeapon.GetComponent(WeaponHandler).weapon1;
     //currentSelected = weapon;
     
     
 }
Comment

People who like this

0 Show 3
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 iwaldrop · Jan 23, 2013 at 08:15 PM 0
Share

You haven't asked a question.

avatar image Loius · Jan 23, 2013 at 08:26 PM 0
Share

"How to set" isn't a question. I don't think anyone on this site has ever asked "How DO I", though, so don't worry about it.

avatar image ChrisHawthorn · Jan 23, 2013 at 10:02 PM 0
Share

Sorry, I guess I meant, this is what I've got so far, hopefully you can see what I'm trying to do, but it doesn't work. So my question would be, what am I doing wrong?

2 Replies

  • Sort: 
avatar image

Answer by Loius · Jan 23, 2013 at 08:28 PM

If you're asking the almost-question in the title:

someGameObject.GetComponent(TypeName).variable = aValue;

or

aVariable = someGameObject.GetComponent(TypeName).variable;

GetComponent is "slow", so if you're going to use it a lot it's better to cache the value as a reference:

var typeName : TypeName = someGameObject.GetComponent(TypeName);

typeName.variable = value;

Comment

People who like this

0 Show 0 · 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

Answer by ChrisHawthorn · Jan 23, 2013 at 10:55 PM

Thanks I tried that out but had no success, I ended up trying something else which I thought would work.

Problem is it now says "An instance of type "WeaponHandler" is required to access non static member 'weapon1'". I changed the weapon1 variable to static, compiled, but of course changes everything, any idea for a solution around this? I'm not sure how to set the appropriate access between it all.

 #pragma strict
 
 var weapon : GameObject;
 var currentClip : float;
 var currentAmmo : float;
 var currentWeapon : GameObject;
 
 function Start ()
 {
 
 }
 
 function OnTriggerEnter (other : Collider) 
 {
     var weaponClone : GameObject = Instantiate (weapon); //clone the weapon
     print("Item is in range");
     WeaponHandler.weapon1 = weaponClone;
     WeaponHandler.weapon1 = currentWeapon;
Comment

People who like this

0 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 iwaldrop · Jan 23, 2013 at 11:22 PM 0
Share

Besides it being bad form to submit answers to your own questions, it's not much of an answer if it doesn't work, is it?

avatar image ChrisHawthorn · Jan 23, 2013 at 11:28 PM 0
Share

I'm just trying to figure it out, it wasn't meant as an answer, just maybe progress that I could use a hand with.

avatar image iwaldrop · Jan 24, 2013 at 12:16 AM 0
Share

That's cool...but questions with more than one answer tend to get ignored because two is usually sufficient.

avatar image ChrisHawthorn · Jan 24, 2013 at 12:22 AM 0
Share

Yea that makes sense, I'm still new the forums and the layout is kinda weird to me. I guess I'll just learn my way around. Thanks for the tip though, I won't make that mistake again :')

Unity Answers is in Read-Only mode

Unity Answers content will be migrated to a new Community platform and we are aiming to launch a public beta by June 9. Please note, Unity Answers is now in read-only so we can prepare for the final data migration.

For more information and updates, please read our full announcement thread in the Unity Forum.

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

How could you access a script of varying name? 5 Answers

how can I display a variable as a GUIText 5 Answers

How to access gameObject variable script 2 Answers

Create a variable on a GameObject for access via the Object without GetComponent() 1 Answer

How can I access a variable from another script in a separate object? 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