- Home /
Toggle a script/key on when collide with object.
I'm wanting to disable a script at the start, and then enable it when I run into the set collider item, my script also destroys the collision item at the same time.
I have everything working, except I can't get it to actually toggle the target script.
This is what I'm trying to do...
Disable the mapToggle key "m" until the player walks over the collision area of the map item that's on the ground and hits the key "e" to pick up/destroy the item on the ground. Thus enabling the use of the "m" key to toggle the map item.
using UnityEngine;
using System.Collections;
public class pickupkey : MonoBehaviour {
public Transform WalkedOverObject;
void Update () {
if(Input.GetKeyDown(KeyCode.E))
{
if (pickupItem.WalkedOverObject) Destroy(pickupItem.WalkedOverObject);
}
}
}
pickupItem is the script that detects if you are near/on an item that is able to be picked up. by your character.
using UnityEngine;
using System.Collections;
public class pickupItem : $$anonymous$$onoBehaviour {
public static GameObject WalkedOverObject;
void Start() {
}
void OnTriggerEnter(Collider other) {
WalkedOverObject = other.gameObject;
}
void OnTriggerExit(Collider other) {
if (WalkedOverObject == this.gameObject) WalkedOverObject = null;
}
}
Answer by Joyrider · Aug 13, 2013 at 06:02 PM
Okay, than something like this, I guess
I'm not checking if the picked up object is a particular object here yet though. And I'm assuming here, the mapToggle script is on the same object as the pickup script.
using UnityEngine;
using System.Collections;
public class pickupkey : MonoBehaviour
{
public GameObject mapObject;
private mapToggle mapToggleScript;
void Start()
{
mapToggleScript = mapObject.GetComponent<mapToggle>();
if(mapToggleScript!=null)
{
mapObject.renderer.enabled = false; // or mapToggleScript.Start(); if Start is set public
mapToggleScript.enabled = false;
}
}
void Update ()
{
if(Input.GetKeyDown(KeyCode.E))
{
if (pickupItem.WalkedOverObject!=null)
{
if(mapToggleScript!=null)
mapToggleScript.enabled = true;
Destroy(pickupItem.WalkedOverObject);
}
}
}
}
Little correction on pickupItem script
using UnityEngine;
using System.Collections;
public class pickupItem : MonoBehaviour {
public static GameObject WalkedOverObject;
void Start() {
}
void OnTriggerEnter(Collider other)
{
WalkedOverObject = other.gameObject;
}
void OnTriggerExit(Collider other)
{
if (WalkedOverObject == other.gameObject) WalkedOverObject = null;
}
}
The mapToggle script is on the item that is being toggled, the pickupkey and pickupItem scripts are both on the graphics of the first person controller as to have a collision area detection.
This is what I'm trying to do...
Disable the mapToggle key "m" until the player walks over the collision area of the map item that's on the ground and hits the key "e" to pick up/destroy the item on the ground. Thus enabling the use of the "m" key to toggle the map item.
Okay, added variable to set the mapObject in, and corrected something line 16 of the pickupItem script, changed this to other.
you could also just make the mapToggleScript variable public and assign it in the inspector by drag'n'dropping the mapObject in that field. Than there would be no need for the newly added mapObjectVariable, nor for the GetComponent
This is only doing what I had it doing, it's still not disabling the key "m" or the script "mapToggle".
It should... that is what mapToggleScript.enabled = false;
is for. Did you assign the map object to the mapObject variable in the inspector?
I see what you mean. Now I'm running into the issue of the map being visible at the start. That may be another problem because this is the script of the item that's starting disabled but needs one key component to still be working at the void start....the renderer.enable=false; part. this is the coding attached to my toggle map component.
using UnityEngine;
using System.Collections;
public class mapToggle : $$anonymous$$onoBehaviour {
public Character$$anonymous$$otor char$$anonymous$$ot;
void Start () {
renderer.enabled=false;
}
void Update () {
if(Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.$$anonymous$$))
{
renderer.enabled = !renderer.enabled;
char$$anonymous$$ot.canControl = !renderer.enabled;
}
}
}
Answer by CypherGames · Aug 08, 2013 at 07:32 PM
Try this as your script:
using UnityEngine;
using System.Collections;
public class pickupItem : MonoBehaviour {
public Transform WalkedOverObject;
public mapToggle toggleScript;
void Start() {
toggleScript = WalkedOverObject.GetComponent<mapToggle>();
toggleScript.active = false;
}
void OnTriggerEnter(Collider other) {
Destroy(other.gameObject);
toggleScript.active = true;
}
}
I tried this, and I updated the coding a bit, but I still can't make it to where the script is disabled first, then after using the "e" command to pick up the item enables the script.
Hmmm