[C#] Objects Appearing at Certain Distances?

Hey everyone!

This is my post here, so i’m not so sure if this is the right place to post this. Sorry if it isn’t.

Anyways, I was wondering if anyone could help me with my problem. I’m trying to make a game where as you walk into a certain distance from a object the object appears. I also need a way to disable the graphics on the object before you come into contact with it. I hope that makes sense. If you reply please try to take it slow as I’m just a beginner.

There’s a few ways you could go about this, but the easiest is probably to use a large trigger on your object, and to toggle the objects renderer component on.

So, all visible game objects have a renderer component of some kind (mesh renderer, sprite renderer, etc) what you would do is disable that component at the start, which will make the object invisible.

Set up a large collider around your object, and then use OnTriggerEnter() or OnColliderEnter() as the call to turn the renderer component on.


Once you have this basic setup working, you can then start looking into shaders that you can animate and fade in to make the effect pretty, but that’s slightly more involved. You can access the object’s material via its renderer and then access specific variables within the material’s shader, then animate these variables to achieve a smoth fade in.

You’ll have to do some research into materials, shaders and renderers to do this more advanced step competently. Wouldn’t hurt to know about co routines as well.

Thanks, I’ve got that down. And sorry, this may be really stupid, but what code would I use to enable the renderer? I have the Void OnTriggerEnter(), but I don’t know what to put in the Void.

So I experimented myself, and I came up with this.

public class Appear : MonoBehaviour {

private Collider2D Circle;

// Use this for initialization
void Start () {

Circle = GetComponent();

}

// Update is called once per frame
void Update () {

}

void OnTriggerEnter()
{
Circle.enabled = true;
}
}

The problem is that whenever my characters CircleCollider2D enters the Objects CircleCollider2D nothing happens. Help?

So, you’ll want to get the Renderer componenet and then enable it.

void OnTriggerEnter(Collider other)
{
     Renderer myRenderer = GetComponenet<Renderer>();

     myRenderer.enabled = true;
}

So, I’m not exactly sure what you’re trying to achieve here…

Some background info:

  • GetComponenet<>(); needs to know what object you want to get a component from. If used without an object declared, it will try to get the component from the same object that this script is attached to.
  • If the collider on this object is not enabled, then you will never get into OnTriggerEnter(), because OnTriggerEnter is only called when another collider passes through this objects collider.
  • Check out the collision action matrix on this page. OnTriggerEnter can depend on a specific collision between colliders with & without rigidbodies, as well as whether or not the colliders are set to “is trigger”

So, I wouldn’t mess with enabling colliders at all - just set them up so that you actually get a call to OnTriggerEnter(). After OnTriggerEnter is called, just activate the renderer component.

Hope that helps!

It seems like once the renderer is disabled it can’t be re-enabled.

Can you post your new code?