Search Unity

switching ON/OFF a C# script with a javascript script during runtime.

Discussion in 'Scripting' started by PROTOFACTOR_Inc, Oct 25, 2010.

  1. PROTOFACTOR_Inc

    PROTOFACTOR_Inc

    Joined:
    Nov 15, 2009
    Posts:
    4,054
    //Hi folks,

    if(is there anybody in who can explain me the way to enable or not a C# script while application is playing?)
    {
    answer me back ();
    } else {
    breackMyHeadAgainstTheWall ();
    }

    I'm not joking !!! Indeed, i really need to enable or not the leg animator scrpt according to the isgrounded() function from third person platformer script so that my character can jump or doubleJump without awfull artifacts.
    I only know a lilttle bit of javascript within unity because i'm a 3D artist ...

    Thanks in advance for your answers
     
  2. Chris-Sinclair

    Chris-Sinclair

    Joined:
    Jun 14, 2010
    Posts:
    1,326
    Last edited: Oct 25, 2010
  3. PROTOFACTOR_Inc

    PROTOFACTOR_Inc

    Joined:
    Nov 15, 2009
    Posts:
    4,054
    Well, thanks for your fast reply... But there is a misunderstanding : my javascript and my C# script are on the same game object : My character has the locomotion system script on him and the third person platformer script for moving and triggering animations. My problem is that when i jump it happens weird ugly artifacts and jump animations don't play correctly ( and i know that's because of the leg animator C# script. So how can i ( within my third person platformer javascript) :


    var leganimator : legAnimator = GetComponent(legAnimator); // here i want to get the leg animator C# script ...
    function LateUpdate()
    {
    if(jumping())
    {
    leganimator.enabled = false;
    } else {
    leganimator.enabled = true;
    }
    }

    // when i do that i've got an error it can't find the component legAnimator
    // I browse the forum and i saw that the C# script must be in the standard asset folder so i moved it but i had the same error ...
    //Someone could help ?
     
  4. Ostagar

    Ostagar

    Joined:
    Sep 29, 2010
    Posts:
    445
    Accessing C# scripts in UnityScript and vice-versa is messy! Do you really need both languages for this?

    I'll assume yes. First, make sure you've put the files in the right places per this document:
    http://unity3d.com/support/documentation/ScriptReference/index.Script_compilation_28Advanced29.html

    Next, try this code. It works with both scripts are components on the same object:

    C#:
    Code (csharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class FrameCounter : MonoBehaviour {
    5.    
    6.     public int frameCount = 0;
    7.    
    8.     // Update is called once per frame
    9.     void Update () {
    10.         if ( (++frameCount % 100) == 0) {
    11.             Debug.Log( "frameCounter = " + frameCount );
    12.         }
    13.     }
    14.    
    15. }
    UnityScript:

    Code (csharp):
    1. function Update () {
    2.     if ( Input.GetKeyDown( KeyCode.S ) ) {
    3.        var fc : FrameCounter;
    4.        fc = gameObject.GetComponent("FrameCounter");
    5.        fc.enabled = false;
    6.     }
    7. }
    You will of course need to modify this code for your purposes, but it illustrates the general idea.
     
  5. PROTOFACTOR_Inc

    PROTOFACTOR_Inc

    Joined:
    Nov 15, 2009
    Posts:
    4,054
    thanks for your reply ! i'll try to adapt your code to my purpose.
    regards
     
  6. SpaceFiveFive

    SpaceFiveFive

    Joined:
    Nov 6, 2013
    Posts:
    27
    For some reason, I am still able to rotate left and right. Ideas?
     
  7. jackmott

    jackmott

    Joined:
    Jan 5, 2014
    Posts:
    167
    Suggesting converting the javascript to c# or vice versa. It will be an instructive exercise, and make life a lot simpler.