- Home /
Affect Script Variables using Triggers
Hello,
I am trying to implement a feature into a first person shooter where certain areas decrease the accuracy of the player. Right now the script on the weapon has two open variables which control the accuracy in the x and y direction.
var projectile : Rigidbody; var speed = 20; var randomMin = -0.5; var randomMax = 0.5;
function Update () { if( Input.GetButtonDown("Fire1") ) { var instantiatedProjectile : Rigidbody = Instantiate( projectile, transform.position, transform.rotation );
var randomNumber = Random.Range(randomMin, randomMax);
instantiatedProjectile.velocity =
transform.TransformDirection( Vector3( randomNumber, randomNumber, speed) );
Physics.IgnoreCollision( instantiatedProjectile. collider,
transform.root.collider ) ;
}
}
I want a trigger to change the variables randomMin and randomMax, but I'm not sure how I can make the trigger change the open variables in the launcher script. I guess in short, I am trying to call a script and change open variables using OnTriggerEnter.
Thanks in advance!
Answer by Jesse Anders · Nov 27, 2010 at 10:31 PM
Use GetComponent() to acquire a reference to the script/component, and then modify the variables as needed.
Answer by azzogat · Nov 28, 2010 at 09:34 AM
You can check the answers from this question for a more detailed explanation:
http://answers.unity3d.com/questions/29163/getcomponent-keeps-returning-null
Your answer
Follow this Question
Related Questions
Noob c# question 1 Answer
How to call Variable across scripts. 2 Answers
Variable from String 0 Answers
can't call script variable from another script 1 Answer
How to create a custom "On..." method? 2 Answers