- Home /
How to go about modifying a variable in a C# script from within a UnityScript (Javascript) that resides in a different Gameobject?
Hello, this is my first question ever asked on here. I even made an account just for this, because so far I could figure everything out myself in Unity or by Googling, but honestly I am a little stumped with this one. This is long, but I never really needed to ask one before, so I wanted to be a good student and ask impartially, rather than demanding things in all caps (like some of the people on here...lol).
Basically, I have an AIFollow (C# Script) on my Enemy making him follow the Player. There is a variable in that script called distance which when the player is within the bounds of that distance, he will start to follow the player and try to attack etc.
Now I have a Point Light object as a child of the Player (so it is not related to this AI at all really) and it has a script for this light called lighting (which makes it a flickering flashlight that you can toggle on and off with the F key, it's pretty cool).
Anyways, the main problem/question is I simply would like to make it that when (inside of my lighting script) the flashlight is on (i.e. the Point Light is enabled) that the distance variable in my C# script will change to a smaller number, making it so that when you turn off the flashlight it makes the Enemy not be able to see you unless you are closer to him.
The distance variable only needs to be changed back and forth between two values, really. A value for when the flashlight is on and one for when the flashlight is off.
I looked around a lot and saw some people saying I need to place my scripts in different folder locations, based on the Unity Compilation order. However, I tried to follow this but I ended up with more errors from moving scripts, which is more confusing. I did my research for the most part and read the references, but as a newbie, some simple problems stump me.
I even tried writing my lighting script in C# so that I could easily edit public variables, but my C# skills are abysmal...
I know that in UnityScript you can simply make a variable static and then just call on the object and script name or whatever, and I use that quite a bit in fact, and I was wondering if that's also the case in C#?
The lighting script is really not that long, I tried to convert it to C# to help me understand the different language better, but the conversion left me with a bunch of errors which I know are probably in simple formatting but since I am not knowledgeable in C# yet I could not fix them myself.
I guess when it boils down to it, should I keep my lighting as unityscript (javascript) and use some sort of folder placement for the compilation and then call on it within my C# AI follow script in order to adjust the distance?
or
Should I change my lightning script to C# and use a different method (hopefully similar to the JS one) to modify and call public variables of different Gameobjects?
I'm really unsure which is more feasible, and it really is hard to search for such specific solutions.
I really do see a lot of needy questions posted by kids who just want you to do everything for them, like "convert this script for me" and such, and I am by no means trying to be like them! I will gladly do more research, and or read sections of manuals for the solution. Just point me in the right direction, please. It seems like it shouldn't be a hard fix, and I am sure someone out there knows better than me.
If someone can give me a little insight to what I should do for this problem, I will gladly put you in the credits of my game! (It's a really creepy horror FPS, and I'll email you a free download to it too!)
Thanks, I hope this wasn't too long of a question, I wanted to be a specific as possible.
This is my lighting script, I'll post the AIFollow script if you need it:
var minFlickerSpeed : float = 0.1;
var maxFlickerSpeed : float = 1;
var minflickerafter : float = 0.1;
var maxflickerafter : float = 1;
var minLightIntensity : float = 0;
var maxLightIntensity : float = 1;
var flickon : AudioClip;
var flickoff : AudioClip;
static var toggledist = 4;
light.enabled = false;
static var toggle = 1;
private var abletotoggle = 0;
function Update (){
if (Input.GetKeyUp("f") && toggle == 0 && light.enabled == true && abletotoggle == 1){
audio.clip = flickoff;
audio.Play();
light.enabled = false;
abletotoggle = 0;
toggle = 1;
return;
}
if (Input.GetKeyUp("f") && toggle == 1 && light.enabled == false && abletotoggle == 0){
audio.clip = flickon;
audio.Play();
light.enabled = true;
toggle = 0;
abletotoggle = 1;
Flicker ();
}
}
function Flicker (){
if (toggle == 0)
{
light.enabled = true;
light.intensity = 1;
abletotoggle = 1;
yield WaitForSeconds (Random.Range(minFlickerSpeed, maxFlickerSpeed ));
light.enabled = false;
abletotoggle = 0;
yield WaitForSeconds (Random.Range(minflickerafter, maxflickerafter ));
light.enabled = true;
abletotoggle = 1;
yield WaitForSeconds (Random.Range(minflickerafter, maxflickerafter ));
light.enabled = false;
abletotoggle = 0;
yield WaitForSeconds (Random.Range(minflickerafter, maxflickerafter ));
light.enabled = true;
abletotoggle = 1;
yield WaitForSeconds (Random.Range(minflickerafter, maxflickerafter ));
light.enabled = false;
abletotoggle = 0;
yield WaitForSeconds (Random.Range(minflickerafter, maxflickerafter ));
light.enabled = true;
abletotoggle = 1;
light.intensity = 1;
}
}
---end of code---
Answer by Pandiux · Aug 18, 2012 at 07:20 AM
If you want to modify a C# from a JS, put the C# script in the Standard Assets folder so it compiles before your JS script, then use a simple GetComponent :), it won't work if it isn't in the Standard Assets.
THAN$$anonymous$$ YOU IT WOR$$anonymous$$S!! I just put the whole folder of pathfinding scripts in standard assets and I didn't get any errors. I love getcomponent now, it's so useful. Thanks a lot man, this really helped me.
Your answer
Follow this Question
Related Questions
Problem with translating C# variable to JS. 2 Answers
Assigning a unity component variable issue 1 Answer
converting javascript to c# 1 Answer
Clicking on an Object to Make it the Variable Target 1 Answer
javascript equivalent of Action? 0 Answers