Search Unity

Mesh Renderer Script

Discussion in 'Scripting' started by mrw3bby, Sep 2, 2010.

  1. mrw3bby

    mrw3bby

    Joined:
    Sep 1, 2010
    Posts:
    18
    Im new to this, especially the scripting. I need a script that works out the distance between an object and the main camera and then turns on or of the mesh renderer of that object, as a form of streaming media. I have been using uniLOD but it doesnt support objects with multiple materials. Any help?
     
  2. andeeeee

    andeeeee

    Joined:
    Jul 19, 2005
    Posts:
    8,768
    You can get the distance between two objects with Vector3.Distance:-
    Code (csharp):
    1. var distance = Vector3.Distance(obj1.transform.position, obj2.transform.position);
    2.  
    3. if (distance > threshold) {
    4.   obj2.renderer.enabled = false;
    5. }
     
  3. mrw3bby

    mrw3bby

    Joined:
    Sep 1, 2010
    Posts:
    18
    Thanks but i cant add script to object, "script has not finished compilation".
    3 errors i get in the console are:
    -An instance of type 'UnityEngine.Component' is required to access non static member 'transform'.
    - 'transform' is not a member of 'String'.
    - 'renderer' is not a member of 'String'.

    Code (csharp):
    1. var distance = Vector3.Distance(Camera.transform.position, "Pub_Main".transform.position);
    2.  
    3. if (distance > 30) {
    4.   "Pub_Main".renderer.enabled = false;
    5. }
    6.  
    Please help, again thanks.
     
  4. Chris-Sinclair

    Chris-Sinclair

    Joined:
    Jun 14, 2010
    Posts:
    1,326
    You can't grab your game objects imply by using a string that way:
    Code (csharp):
    1. var target = GameObject.Find("Pub_Main");
    2.  
    3. var distance = Vector3.Distance(Camera.transform.position, target.transform.position);
    4.  
    5. if (distance > 30)
    6. {
    7.     target.renderer.enabled = false;
    8. }
     
  5. mrw3bby

    mrw3bby

    Joined:
    Sep 1, 2010
    Posts:
    18
    Thanks that fixes all but the first error. hmmm :?
     
  6. Fenrisul

    Fenrisul

    Joined:
    Jan 2, 2010
    Posts:
    618
    Camera.main.transform.position
     
  7. mrw3bby

    mrw3bby

    Joined:
    Sep 1, 2010
    Posts:
    18
    sorry guys but yet another error has occured.
    "UnityException: You are not allowed to call this function when declaring a variable.
    Move it to the line after without a variable declaration.
    If you are using C# don't use this function in the constructor or field initializers, Instead move initialization to the Awake or Start function."

    An explaination to the solution would also help so i can avoid the problem in the future... Thanks
     
  8. andeeeee

    andeeeee

    Joined:
    Jul 19, 2005
    Posts:
    8,768
    Would I be right in thinking you have added the posted code directly into a script file? It was just intended as an example of the basic code structure required - you actually need to put this code inside the Update function for it to work correctly. You probably want something like this:-
    Code (csharp):
    1. var target: GameObject;
    2.  
    3. function Start() {
    4.   var target = GameObject.Find("Pub_Main");
    5. }
    6.  
    7. function Update() {
    8.   var distance = Vector3.Distance(Camera.transform.position, target.transform.position);
    9.  
    10.   if (distance > 30)
    11.   {
    12.      target.renderer.enabled = false;
    13.   }
    14. }
     
  9. mrw3bby

    mrw3bby

    Joined:
    Sep 1, 2010
    Posts:
    18
    Thanks guys finally got the script working... And learned a few things in the process... If anyone wants the final script here it is...
    Code (csharp):
    1. var target: GameObject;
    2.  
    3. function Start() {
    4.   var target = GameObject.Find("Pub_Main");
    5. }
    6.  
    7. function Update() {
    8.   var distance = Vector3.Distance(Camera.main.transform.position, target.transform.position);
    9.  
    10.   if (distance > 50)
    11.   {
    12.      target.renderer.enabled = false;
    13.   }
    14.  
    15.     if (distance <50)
    16.   {
    17.      target.renderer.enabled = true;
    18.   }
    19.  
    20. }
    One last thing in order to make this script for general use not just for "Pub_main", I would like to make the distances editable within Unity inspector, target already is.
    Again Thanks.