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 KM Studios · Aug 18, 2012 at 06:57 AM · c#javascriptvariablemodify

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---

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 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.

Comment
Add comment · Show 1 · 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 KM Studios · Aug 18, 2012 at 06:11 PM 0
Share

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

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

9 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


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