Search Unity

[SOLVED] GameObject.GetComponent Problem (JavaScript)

Discussion in 'Scripting' started by aaaaaieeeeeeeee, Aug 17, 2014.

  1. aaaaaieeeeeeeee

    aaaaaieeeeeeeee

    Joined:
    Aug 17, 2014
    Posts:
    9
    Hello i got a Simple problem.
    I'm avoiding to use the .GetComponent in my Update function. I got some really bad performance because i used to much of these. I will make a example.

    Code (JavaScript):
    1. var thePlayer : GameObject
    2. var theDoor : GameObject
    3.  
    4. function Update()
    5. {
    6. var ray : Ray = thePlayer.camera.ViewportPointToRay (Vector3(0.5,0.5,0));
    7.     var lenght : float = 5.0;
    8.     var hit : RaycastHit;
    9.     if(Physics.Raycast(ray, hit, lenght))
    10.    
    11.     {
    12.        
    13.         hitObject = hit.collider.gameObject;
    14.        
    15.         if(hitObject.gameObject.tag == "Door")
    16.                    
    17.          {
    18.                 if(Input.GetKeyDown(KeyCode.E))
    19.                 {
    20.                 Attacked();
    21.                 }
    22.          }
    23.     }
    24. }
    25. function Attacked ()
    26. {
    27. player.camera.enabled = false;
    28. cameraSnow.SetActive (true);
    29. yield WaitForSeconds (0.01);
    30. player.camera.enabled = true;
    31. cameraSnow.SetActive (false);
    32. theDoor.GetComponent(RandomScript).enabled = true;
    33. }
    So that's just one random example. My actuall script is much larger that's why i need to define the .GetComponent types in the Start or Awake function and call enable/disable them in a Update function. However I'm pulling my hair out of this. I didn't figured out how you actually do this in Javascript. It has plenty of answers in C# but yeah.. I searched about 1 hour in Google for the answer and checked the Scripting Reference.. I even posted my first question on Unity. So either I am completely
    incompetent or just stupid. Im sorry for bothering you guys and i also apologize for my bad english..
     
  2. Magiichan

    Magiichan

    Joined:
    Jan 5, 2014
    Posts:
    403
    What's your problem?
    use gameObject.GetComponent("scriptName");
    to get the script.
     
  3. aaaaaieeeeeeeee

    aaaaaieeeeeeeee

    Joined:
    Aug 17, 2014
    Posts:
    9
    Yes i would like to but in my real script i use it that much, It can't call everything on every frame and some commands don't even get executed and im just standing there, without an error nothing.
     
  4. Magiichan

    Magiichan

    Joined:
    Jan 5, 2014
    Posts:
    403
    put #pragma strict at the top of your code, do you still get no errors like this?
     
  5. TRG96

    TRG96

    Joined:
    Mar 26, 2011
    Posts:
    102
    Make a public variable with the same name as your script from which you want the variable from and in the inspector just drag and drop the second script. In your example you have a RandomScript variable, in your other script have this code

    public var randomScript : RandomScript

    and in the inspector you should be able to drag and drop the script or you can do the following

    function start()
    {
    randomScript = GameObject.Find("Door").GetComponenet("RandomScript");
    }

    that will get the script and set the script variable to that and you should be able to access its variables
     
    aaaaaieeeeeeeee likes this.
  6. aaaaaieeeeeeeee

    aaaaaieeeeeeeee

    Joined:
    Aug 17, 2014
    Posts:
    9
    I got one with the raycast..
    Hmm. It seems like hitObject is an unknown identifier..
     
  7. aaaaaieeeeeeeee

    aaaaaieeeeeeeee

    Joined:
    Aug 17, 2014
    Posts:
    9
    Yes that's exactly what I wanted but the final question is if i use you second example with the start function, how do i actually enable and disable that component?

    I tried this before with like :

    randomScript.enabled = false;

    am i just stupid or is this really not working?
     
  8. Magiichan

    Magiichan

    Joined:
    Jan 5, 2014
    Posts:
    403
    @SuperBlackMario1
    Code (JavaScript):
    1. #pragma strict
    2. var targetScript : MyClass;
    3.  
    4. function Start() {
    5.      targetScript = GetComponent("MyClass");
    6.      targetScript.enabled = false;
    7. }
     
    aaaaaieeeeeeeee likes this.
  9. aaaaaieeeeeeeee

    aaaaaieeeeeeeee

    Joined:
    Aug 17, 2014
    Posts:
    9
    Thanks, I can finally rest in peace now.
     
    Magiichan likes this.
  10. Magiichan

    Magiichan

    Joined:
    Jan 5, 2014
    Posts:
    403
    Goodluck with your game c;
     
    aaaaaieeeeeeeee likes this.
  11. Target786

    Target786

    Joined:
    Dec 8, 2018
    Posts:
    1
    Hello i got a Simple problem.
    I'm avoiding to use the .GetComponent in my function. I got some really bad performance because i used to much of these. I will make a example.


    function FireOneShot () {

    //No shooting when sprinting
    if (canShoot){
    var target = transform.GetComponenet("AI").Target;


    (Assets/!Realistic FPS Prefab Files/~Demo Scene Assets/~AlienNPC/Scripts/Gun.js(102,40): BCE0019: 'GetComponenet' is not a member of 'UnityEngine.Transform'. )
     
  12. KISP

    KISP

    Joined:
    Sep 13, 2018
    Posts:
    32
    Could you clarify your question? Are you asking what that error message means, or are you trying to remove the GetComponent line completely?