- Home /
Converting C# to JS: rendering a mesh
So this is what I have:
GetComponent(MeshRenderer).enabled = false;
I got this from another question, but the rest of my script is Java and I was wondering if anyone knows a way to do this in Java rather than c#
basically, I have a box of matches, which I collect and after I collect it, what's SUPPOSED to happen is a note pops up on the screen when I get near the fireplace, telling me to use my matches. I then press "e" and voila, a fire appears...
Here is my script:
#pragma strict
var buttonInRange;
var buttonActivated;
var batterySound : AudioClip;
var Transparent : Material;
var Matches : GameObject;
var Fire : GameObject;
private var hasPlayed = false;
function Start ()
{
//THIS IS WHERE I NEED HELP
GetComponent(MeshRenderer).enabled = false;
}
function OnTriggerEnter (c : Collider)
{
buttonInRange = true;
}
function OnTriggerExit (c : Collider)
{
buttonInRange = false;
}
if (buttonInRange == true)
{
if (Matches == false)
{
GUI.Label (Rect (Screen.width/2-50, Screen.height/2-55, 120, 50), "Use matches");
}
}
}
function Update ()
{
if (buttonInRange == true)
{
if (Input.GetKeyDown ("e"))
{
AudioSource.PlayClipAtPoint(batterySound, transform.position);
//I ALSO NEED HELP HERE
GetComponent(MeshRenderer).enabled = false;
}
}
}
The problem I'm having is I can't make the fire start off invsible when the scene loads and become visible when I click "e" and have collected my matches.
If it helps, here is my match script:
var buttonInRange;
var buttonActivated;
var batterySound : AudioClip;
private var hasPlayed = false;
function OnTriggerEnter (c : Collider)
{
buttonInRange = true;
}
function OnTriggerExit (c : Collider)
{
buttonInRange = false;
}
function OnGUI ()
{
if(buttonInRange == true)
{
GUI.Label (Rect (Screen.width/2-50, Screen.height/2-55, 120, 50), "Pick up matches");
}
}
function Update ()
{
if (buttonInRange == true)
{
if (Input.GetKeyDown ("e"))
{
if(!hasPlayed)
{
AudioSource.PlayClipAtPoint(batterySound, transform.position);
Destroy(gameObject);
hasPlayed = true;
}
}
}
}
Any help is much, much appreciated :D
Answer by Cherno · Oct 17, 2016 at 09:06 AM
You are accessing the MeshRenderer of the GameObject that the first script is on, which I assume is on the player character. What you want to do instead is access the MR of the Fire GameObject.
function Start ()
{
Fire.GetComponent(MeshRenderer).enabled = false;
}
..and so on.
So I just tried that, and it didn't work. I originally had the first script attached to the fire itself, but the tried it on the first person controller as per your suggestion. Unfortunately, neither worked...
Thanks for trying though, If I work it out I'll let you know
I suggest doing some debugging. Are you sure that all the parts of your code are actually reached when they should? Insert Debug.Log lines to find out and print results to the console.
Your answer
Follow this Question
Related Questions
Only render part of object inside a circle 2 Answers
How do I make one object appear when I mouse over another object? 1 Answer
Rendering with cubes/planes/matrices 2 Answers
Mouse over make object Visible 2 Answers
[javascript] detect when object is seen by camera, Renderer.isVisible not working 0 Answers