- Home /
access a var from another script
I've seen this question asked dozens of times and have yet to find a solution to my problem. I'm new to JS and Unity as well so bear with me while I explain the situation.
I have two Scripts and a GameObject and a Prefab.
(script1)
The GameObject on GetButtonUp Fire2 (right-click) Instantiates a PreFab. After 15 PreFabs have been created I destroy the first one after each consecutive click.
(script2)
I now want the spawned PreFab to also destroy after a specific collision. The destruction works fine. My problem is that when the PreFab gets destroyed via a collision it doesn't know it in the script of the GameObject that spawns it.
What I am looking for:
If the object gets destroyed via a collision I want it to tell script1 that it has done so.
my code so far (not sure how to post code in a forum yet):
(Script 1, 'Creator', applied to empty gameObject 'Gun')
var thePrefab : GameObject;
public var MaxObjectCount = 0;
var ForceX : float = 500.0;
var ForceY : float = 500.0;
var ForceZ : float = 500.0;
function Update () {
//spawn box
if(Input.GetButtonUp("Fire2")){
var instance : GameObject = Instantiate(thePrefab, transform.position, transform.rotation);
//velocity of spawned box
instance.rigidbody.AddForce(Vector3(ForceX,0,0));
instance.rigidbody.AddForce(Vector3(0,ForceY,0));
instance.rigidbody.AddForce(Vector3(0,0,ForceZ));
instance.name = "boxSpawn";
//add to the total spawned
MaxObjectCount += 1;
checkhit();
}
}
function checkhit(){
//after 15 box's have spawned kill the next one and create a new one
if(MaxObjectCount == 16){
Destroy(GameObject.Find("boxSpawn"));
//subtract from the total spawned
MaxObjectCount -=1;
}
}
(Script 2, applied to PreFab in Project Pane named 'BoxPrefab')
function Update (){
//not sure what to put here
var killCount : Creator = gameObject.GetComponent(Creator);
}
//destroys the spawned box upon collision
function OnCollisionEnter (boxHitKill : Collision) {
if(boxHitKill.gameObject.name == "CollisionWallDestroyer"){
Destroy(GameObject.Find("boxSpawn"));
//where I want to subtract from the variable MaxObjectCount on the script 'Creator'
Creator.MaxObjectCount -=1;
}
}
How can I access the current status of the variable 'MaxObjectCount' and affect it in Script2? I've read the documentation and a lot of forums posts but have yet to be able to make changes from one script onto another one since the second script is attached to a prefab that hasn't been spawned yet. I'm sure the solution is simple, but I am new to the scene and appreciate any help I can get. Thank you in advance!
Answer by FTheCloud · Aug 19, 2011 at 09:13 AM
I say the same thing every time someone asks this question
Answer by betabob-2 · Aug 19, 2011 at 05:33 PM
My latest attempt for Script 2:
var killCount : Creator;
function OnCollisionEnter (boxHitKill : Collision) {
if(boxHitKill.gameObject.name == "CollisionWallDestroyer"){
Destroy(GameObject.Find("boxSpawn"));
killCount.GetComponent(Creator).MaxObjectCount -=1;
}
}
The problem I have now (I think) is that I need to link the gameObject that has the creator script on it in the inspector. But since my object hasn't spawned I can't do that in the project pane and haven't figured out how to link it as soon as it's spawned.
I also dropped the update() function because I wasn't sure if I could setup a var in one function and have it be called in another, is that possible?
function Update(){
var killCount : Creator = gameObject.GetComponent("Creator");
}
function OnCollisionEnter (boxHit$$anonymous$$ill : Collision) {
if(boxHit$$anonymous$$ill.gameObject.name == "CollisionWallDestroyer"){
Destroy(GameObject.Find("boxSpawn"));
killCount = gameObject.GetComponent(Creator).$$anonymous$$axObjectCount -=1;
}
}
$$anonymous$$y latest attempt, still just a variation from what I've been doing, have tried all kinds of other stuff but still no luck. I just can't get my prefab that spawns to ever actually 'get' the component or variable after it's created. I also added this script to the "gun" gameObject but even there it doesn't update the component or show the variable a 2nd time as I was expecting.
Any suggestions would be greatly appreciated.
Answer by Spartan301 · Aug 23, 2011 at 01:45 AM
To answer your question about linking the creator script to the prefab object, all you need to do is select the prefab object in the project bar and drag the 'Gun' object to the killCount variable part of the script component. Also, you may be getting errors when you write
var killCount : Creator;
since creator I don't beleive is a valid variables type. Instead try
var killCount : Transform;
Finally when you use GetComponent, make sure to put the name of the guns script in quotation marks (""), otherwise it wont work. I think this should work, unless I am not fully understanding your situation.
Ok, I think we are getting closer to the solution with this.
var killCount : Transform;
function OnCollisionEnter (boxHit$$anonymous$$ill : Collision) {
if(boxHit$$anonymous$$ill.gameObject.name == "CollisionWallDestroyer"){
Destroy(GameObject.Find("boxSpawn"));
killCount = gameObject.GetComponent("Creator").$$anonymous$$axObjectCount -=1;
}
}
I wasn't able to drag the Gun gameObject onto the script when it was still in the project pane, so I just placed it beneath my scene and that worked. So now my bullets spawn ok, they have the Gun script attached correctly but when the GetComponent occurs it tells me "NullReferenceException: Object reference not set to an instance of an Object". So where killCount = gameObject.GetComponent("Creator").$$anonymous$$axObjectCount -=1;
killCount is my var, "Creator" is my component and $$anonymous$$axObjectCount is the var within that component I am trying to access, no luck on that last part yet.
Your answer
Follow this Question
Related Questions
Javascript score problems whilst referencing scripts. 2 Answers
Specific enemy variable affects all enemies 2 Answers
public script variable in inspector 2 Answers
Set prefab to public variable of another script 1 Answer
Value to 0 on collision 1 Answer