How do i make a script that makes the gameobject enabled,
So i just got a kit from MasterDevelopers and I'm trying to edit a script when if you collect 1 page, Slender man will appear in the woods. (The game object will be enabled) I tried other solutions but they don't work. Does anyone have a solution?
Code: JavaScript Version: 4.6
/// created by MasterDevelopers /// #pragma strict @script RequireComponent( AudioSource )
var papers : int = 0;
var papersToWin : int = 8;
var papersToSlender : int = 1;
var distanceToPaper : float = 2.5;
public var paperPickup : AudioClip;
var theEnemy : EnemyScript;
function Start()
{
Screen.lockCursor = false;
// find and store a reference to the enemy script (to reduce distance after each paper is collected)
if ( theEnemy == null )
{
theEnemy = GameObject.Find( "Enemy" ).GetComponent( EnemyScript );
}
}
function Update()
{
//if ( Input.GetMouseButtonUp(0) ) // use E in editor as LockCursor breaks with left mouseclick
if ( Input.GetMouseButtonDown(0) || Input.GetKeyDown(KeyCode.E) )
{
//var ray = Camera.main.ScreenPointToRay( Input.mousePosition ); // always cast ray from center of screen
var ray = Camera.main.ScreenPointToRay( Vector3( Screen.width * 0.5, Screen.height * 0.5, 0.0 ) );
var hit : RaycastHit;
if ( Physics.Raycast( ray, hit, distanceToPaper ) )
{
//if ( hit.collider.gameObject.tag == "Paper" )
if ( hit.collider.gameObject.name == "Paper" )
{
papers += 1;
//Debug.Log( "A paper was picked up. Total papers = " + papers );
audio.PlayClipAtPoint( paperPickup, transform.position );
Destroy( hit.collider.gameObject );
// make enemy follow closer
theEnemy.ReduceDistance();
}
}
}
}
function OnGUI()
{
if ( papers < papersToWin )
{
}
else
{
Application.LoadLevel( "Main Menu" );
{
if ( papers < papersToSlender )
{
GameObject.GetComponent(GameObject);
GameObject.transform.active = true;
}
else
{
}
}
}
}
whoops, i made a bit of mistake. don't know if i could fix it
Answer by mhogan256 · May 18, 2018 at 10:50 AM
C# example
for components CameraHolder.SetActive(true);
for gameobjects: charCanvas.GetComponent().enabled = true;
Answer by elandant · May 18, 2018 at 12:17 PM
What mhogan256 said still applies for JavaScript. It would be GameObject.SetActive(true);
Hmm. Do you have a variable for the object you are referencing stored? And are you accessing it directly? Or is it just "GameObject"?
Also could you please attach the line that is causing the error and the variable you have linked to it?
Answer by NETRIDER · Jun 01, 2018 at 09:15 PM
@RealStarlightStudios thanks for postiing this question on my stream. please check your folder structure, the code for detection you have at function start is where I would look at moving around. some js and cs files need to be placed in specific folders to see one another. I hope I can find a code snippet to share. thanks again!