- Home /
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;
}
)
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.
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
}
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
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.
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.