Mesh Renderer Script

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?

You can get the distance between two objects with Vector3.Distance:-

var distance = Vector3.Distance(obj1.transform.position, obj2.transform.position);

if (distance > threshold) {
  obj2.renderer.enabled = false;
}

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’.
var distance = Vector3.Distance(Camera.transform.position, "Pub_Main".transform.position); 

if (distance > 30) { 
  "Pub_Main".renderer.enabled = false; 
}

Please help, again thanks.

You can’t grab your game objects imply by using a string that way:

var target = GameObject.Find("Pub_Main");

var distance = Vector3.Distance(Camera.transform.position, target.transform.position);

if (distance > 30) 
{
	target.renderer.enabled = false;
}

Thanks that fixes all but the first error. hmmm :?

Camera.main.transform.position

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

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:-

var target: GameObject;

function Start() {
  var target = GameObject.Find("Pub_Main"); 
}

function Update() { 
  var distance = Vector3.Distance(Camera.transform.position, target.transform.position); 

  if (distance > 30) 
  { 
     target.renderer.enabled = false; 
  }
}

Thanks guys finally got the script working… And learned a few things in the process… If anyone wants the final script here it is…

var target: GameObject;

function Start() {
  var target = GameObject.Find("Pub_Main");
}

function Update() {
  var distance = Vector3.Distance(Camera.main.transform.position, target.transform.position);

  if (distance > 50)
  {
     target.renderer.enabled = false;
  }
  
    if (distance <50)
  {
     target.renderer.enabled = true;
  }
  
}

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.