Search Unity

problem in accessing another script component

Discussion in 'Scripting' started by galaboy, Dec 22, 2012.

  1. galaboy

    galaboy

    Joined:
    Dec 2, 2012
    Posts:
    8
    hi am doing a fps game using Eteeski's tutorials.
    my problem is am doing it in c# .the logic of the script is that am accessing a line of code named as "currentyrotation" from another script, and there is a declaration problem in the script.

    transform.rotation = Quaternion.Euler(0,cameraobject.GetComponent("mouselookup_script").currentyrotation,0);

    the error message i get when compiling this code is

    Assets/scripts/playermovement_script.cs(17,49): error CS1502: The best overloaded method match for `UnityEngine.Quaternion.Euler(float, float, float)' has some invalid arguments

    how to write this line of code in c#.

    i am even attaching the scripts.

    need help....
     

    Attached Files:

  2. Gibbonator

    Gibbonator

    Joined:
    Jul 27, 2012
    Posts:
    204
    There are two problems here.
    Firstly, in C# the return value of GetComponent() is of type Component. To do what you want you need to use the generic version of GetComponent so it returns the correct type:
    Code (csharp):
    1.  
    2.     transform.rotation = Quaternion.Euler(0,cameraobject.GetComponent<mouselookup_script>().currentyrotation,0);
    3.  
    Secondly, the field currentyrotation is private. If you want to access it outside that script you need to make it public:
    Code (csharp):
    1.  
    2.     public float currentyrotation;
    3.  
     
  3. galaboy

    galaboy

    Joined:
    Dec 2, 2012
    Posts:
    8
    thank you gibbonator, i got it corrected, but when i declare the currentyRotation variable as public it shows up in the inspector view.is there any way of hiding it from inspector like @HideInInspector command in javascript.

    thanks once again for ur help.its working now.
     
  4. KyleOlsen

    KyleOlsen

    Joined:
    Apr 3, 2012
    Posts:
    237
    Code (csharp):
    1.  
    2. // c# version of @HideInInspector
    3. [HideInInspector]
    4. public float currentyRotation;
    5.