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 Cowboy433 · Jun 21, 2015 at 10:55 AM · variablesc# to javascript

Making JS Interact with C# Variables

I need some help with my JS code interacting with my C# variables. I do not know how to make them interact with each other. I would like it to subtract 100 from score if the player is dead, however, if the player kills another player, this player gets 200 points. Here is my code:

c#

 using UnityEngine;
 using System.Collections;
 
 public class ScoringSystemGUI : MonoBehaviour {
     static int score = 0;
     // Use this for initialization
     void Start () {
         //No Start Commands, so leave this area empty (in case a future update requires it)
     }
     
     // Update is called once per frame
     void Update () {
        //This is important................................... xD
       guiText.text = "Score"+score; //Display, on the Graphic User Interface, the score on the top right (or left) of the screen.
     }
 }
 

js

 #pragma strict
 #pragma implicit
 #pragma downcast
 
 class SoldierDamageControl extends MonoBehaviour
 {
     public var life : float;
     public var Respawn : Vector3[];
     public var Parent : GameObject;
     public var hitTexture : Texture2D;
     public var blackTexture : Texture2D;
     public var Skin : GUISkin;
     public var Local : boolean;
     
     public var Soldier : GameObject;
     public var Gun : GameObject;
     
     public var KilledStyle : GUIStyle;
     public var ActorNr : int;
         public var ActorName : String;
     public var DisplayKillYou : boolean;
     
     private var hitAlpha : float;
     private var blackAlpha : float;
     private var AtackerName : String;
     private var actorText : GameObject;
     private var actorTextOffset : Vector3 = new Vector3(0,1.0f,0);
     
     private var recoverTime : float;
     
     public var hitSounds : AudioClip[];
     public var dyingSound : AudioClip;
     var score = 0;
     function SetActorNr(DataActor : Hashtable)
     {
         ActorNr = DataActor["actornr"];
         ActorName = DataActor["actorname"];
         if(actorText == null && gameObject.layer != 13)
         {
             this.actorText = Instantiate(Resources.Load("ActorName"));
             this.actorText.name = ActorNr+"";
             actorText.transform.localScale = new Vector3(1.4 / 8f, 1.4 / 8f, 1.4 / 8f);
         }
         
     }
     
     function Start()
     {
         SoldierController.dead = false;
         hitAlpha = 0.0;
         blackAlpha = 0.0;
         life = 1.0;
         KilledStyle = Skin.GetStyle("Killed");
     }
     
     function HitSoldierGranade(hit : String)
     {
 
         if(GameManager.receiveDamage)
         {
             if(!audio.isPlaying)
             {
                 if(life < 0.5 && (Random.Range(0, 100) < 30))
                 {
                     audio.clip = dyingSound;
                     score =- 200;
                 }
                 else
                 {
                     audio.clip = hitSounds[Random.Range(0, hitSounds.length)];
                 }
                 
                 audio.Play();
             }
          if(gameObject.layer != 13)
          { 
          
                 var _usePhoton : GameObject;
                 _usePhoton = GameObject.Find("_GameManager/UsePhoton");                
                 _usePhoton.SendMessage("DownLifeGranade", ActorNr);
                 
             return;
          }
          
             life -= 1.0;
 
             recoverTime = (1.0 - life) * 10.0;
             
             if(hit == "Dummy")
             {
                 TrainingStatistics.dummiesHit++;
             }
             else if(hit == "Turret")
             {
                 TrainingStatistics.turretsHit++;
             }
             
             if(life <= 0.0)
             {
                 AtackerName = hit;
                 SoldierController.dead = true;
                 DisplayKillYou = true;
                 
             }
             
         }
     }
     
     
     
     
     function HitSoldier(hit : String)
     {
         if(GameManager.receiveDamage)
         {
             //To do Enviar reduccion de life
             if(!audio.isPlaying)
             {
                 if(life < 0.5 && (Random.Range(0, 100) < 30))
                 {
                     audio.clip = dyingSound;
                     score += 200;
                 }
                 else
                 {
                     audio.clip = hitSounds[Random.Range(0, hitSounds.length)];
                 }
                 
                 audio.Play();
             }
         
          if(gameObject.layer != 13)
          { 
  
                 var _usePhoton : GameObject;
                 _usePhoton = GameObject.Find("_GameManager/UsePhoton");                
                 _usePhoton.SendMessage("DownLife",ActorNr);
                 
                 return;
          }
             life -= 0.05;
             
 
             
             recoverTime = (1.0 - life) * 10.0;
             
             if(hit == "Dummy")
             {
                 TrainingStatistics.dummiesHit++;
             }
             else if(hit == "Turret")
             {
                 TrainingStatistics.turretsHit++;
             }            
             if(life <= 0.0)
             {
                 AtackerName = hit;
                 DisplayKillYou = true;
                 SoldierController.dead = true;
      
             }
             
         }
     }
     
     function DisplayCountDown(time : int)
     {
         yield WaitForSeconds (time);
         DisplayKillYou = false;
     }
     
     function Update()
     {
         
         
     
         if(actorText != null)
         {
         
             var textMesh : TextMesh =  actorText.GetComponent(TextMesh);
             textMesh.text = ActorName;
             // text looking into oposite direction of camera
             this.actorText.transform.position = transform.position + this.actorTextOffset;
             var camDiff : Vector3 = actorText.transform.position - Camera.main.transform.position;
             var lookAt : Vector3 = actorText.transform.position + camDiff;
             this.actorText.transform.LookAt(lookAt);
         }
        
         recoverTime -= Time.deltaTime;
         
         if(recoverTime <= 0.0)
         {
             life += life * Time.deltaTime;
             
             life = Mathf.Clamp(life, 0.0, 1.0);
             
             hitAlpha = 0.0;
         }
         else
         {
             hitAlpha = recoverTime / ((1.0 - life) * 10.0);
         }
     
         if(!SoldierController.dead)
         { 
             blackAlpha = 0.0;
  
             return;
         }
         
         
         blackAlpha += Time.deltaTime;
         
         if(blackAlpha >= 1.0)
         {
             life = 1.0;
             hitAlpha = 0.0;
             blackAlpha = 0.0;
             recoverTime = 0;
             if(SoldierController.dead)
             {
                     var _DeadUsePhoton : GameObject;
                     _DeadUsePhoton = GameObject.Find("_GameManager/UsePhoton");
                     _DeadUsePhoton.SendMessage("Dead", Respawn[Random.Range(0, 13)]);        
             }    
             SoldierController.dead = false;
             
             
             //Application.LoadLevel(0);
             //Application.LoadLevel(1);
         }
         
 
         
         if(DisplayKillYou)
         {
             DisplayCountDown(7);
         }
     }
     
     function OnGUI()
     {
         if(DisplayKillYou)
         {
             if(AtackerName != "Player"){
                 GUI.Label(Rect (Screen.width - 200,60,200,50),AtackerName+" killed you",KilledStyle);
             }else
             {
                 GUI.Label(Rect (Screen.width - 200,60,200,50),"killed yourself",KilledStyle);
             }
         }
         if(!GameManager.receiveDamage) return;
         if(gameObject.layer != 13){ 
             return;
         }
 
         var oldColor : Color;
         var auxColor : Color;
         oldColor = auxColor = GUI.color;
         
         auxColor.a = hitAlpha;
         GUI.color = auxColor;
         GUI.DrawTexture(new Rect(0, 0, Screen.width, Screen.height), hitTexture);
         
         auxColor.a = blackAlpha;
         GUI.color = auxColor;
         GUI.DrawTexture(new Rect(0, 0, Screen.width, Screen.height), blackTexture);
         
 
         
         GUI.color = oldColor;
     }    
 }


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

0 Replies

· Add your reply
  • Sort: 

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

2 People are following this question.

avatar image avatar image

Related Questions

Accessing variables from GameObjects in an array 3 Answers

Calling a mesh instance 'mesh' or 'msh' (to avoid the keyword) 1 Answer

Create new variable name based on array index 3 Answers

Is it possible to loop through the variables of a custom object? 1 Answer

Local Variables 2 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