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 ninjaboynaru · Jul 02, 2013 at 11:37 PM · classgunrefer

Refer to object class is attached to

I have a variable inside the weapon class called thisGun that I would like to be set to what ever objects are currently set as the wepaon class.

EX If I had an object called gun1 how would I get the varible thisGun to refer to gun1's transform or GameObject.

Script attached to the gun Object

 var gun : Weapon
  function Start(){
  gun.setValue
  }






Script that contains the Weapon class (bellow)

 class Weapon extends Item{
 
 
 function SetValues(){   //called at function start on gun Object
 var thisGun = this.GameObject;
 
 }
 
 )
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 TonyLi · Jul 02, 2013 at 11:55 PM

See: Accessing Other Game Objects.

If I understand you correctly, you probably want a reference to the Weapon component that's on an object. You can use GetComponent():

 var gun : Weapon;
 
 // Get a reference to the first Weapon component found on the game object:
 gun = GetComponent(Weapon);
 
 // Always check for null, in case none was found:
 if (gun == null) Debug.LogError("No Weapon component found!");
 
 // What the heck, I'm not sure why you'd do this, but
 // this sets the Weapon class's thisGun variable to
 // point to its own game object, since this component
 // is on the same game object:
 gun.thisGun = this.gameObject;

I give 50/50 odds that I understand your question correctly. If I totally missed it, read that link above.

Comment
Add comment · Show 5 · 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 ninjaboynaru · Jul 03, 2013 at 12:05 AM 0
Share

No u did not understand my question but thats my fault, srry. To make this easy to understand This is what I am trying to do

var thisGun = (the object gun1's transform) Also thisGun is located inside a class called Weapon in a different script and gun1 has a script attached to it that goes like this

 var gun.Weapon
 function Start(){
 SetValues()  // this is the function inside the Weapon class
 }



avatar image TonyLi · Jul 03, 2013 at 01:24 PM 0
Share

I still don't understand. Can you post both scripts?

avatar image ninjaboynaru · Jul 04, 2013 at 08:05 PM 0
Share

Gun Class Script

  class Weapon extends Item{
     var range : float;
     var rateOfFire : int;
     var weaponType : weaponType;
     var clipSize : int;
     var fireLocation : Transform;
     var bulletPrefab : ammoType;
     var gWeapon$$anonymous$$anager : GameObject;
     var tempoAmmoPref : GameObject;
     var currentWeapon = (how would I get this variable to equal the game object of the gun object/the AR-15)
     
     function SetValues(){
     gWeapon$$anonymous$$anager = GameObject.Find("Global Weapons Class $$anonymous$$anager");
     Debug.Log(gWeapon$$anonymous$$anager.name);
     }
     }

Script Attached to Gun Object(AR-15)

var gun : Weapon; function Start(){ Gun.SetValues(); }

  function Update(){
  }

How would I get the var currentWeapon to equal the AR-15/Gun Object

avatar image TonyLi · Jul 04, 2013 at 08:55 PM 0
Share

Wow, sorry I'm still not sure that I understand you. If Item is a subclass of $$anonymous$$onoBehaviour, it inherits the gameObject property. You can assign it to currentWeapon like this in Weapon.js:

 var currentWeapon : GameObject;
 
 function Awake() {
     currentWeapon = this.gameObject;
 }

If Item is not a subclass of $$anonymous$$onoBehaviour, then you can modify SetValues like this:

 function SetValues(gameObject : GameObject) {
     currentWeapon = gameObject;
 }

And do this in your $$anonymous$$onoBehaviour script:

 function Start() {
     Gun.SetValues(gameObject);
 }

If this doesn't answer your question, please post your game object hierarchy.

avatar image ninjaboynaru · Jul 07, 2013 at 01:42 AM 0
Share

Thanks for all your help.

This is what solved it for me

     var currentWeapon : GameObject;
      
     function Awake() {
     currentWeapon = this.gameObject;
     }


Except I did it in function start, and I put the currentWeapon varible in the class script.

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

16 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

Related Questions

Problems with Classes...i think 1 Answer

CoD 4 "weapon classes" 2 Answers

Spawning GameObjects with help of classes --- Attaching classes to GameObjects 1 Answer

How do I create separate instances from a class? 2 Answers

How to get a List of all Variables in a Class 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