- Home /
Move a wall up when player steps on trigger
So I have a ball that I can control. When I roll it over a box I want a wall next to it to raise so the ball can pass. How do I access/invoke a method in a script that is placed on the wall object via another script placed on the trigger?
Here are my scripts:
doorTriggerScript.cs (placed on the trigger box):
using UnityEngine;
using System.Collections;
public class doorTriggerScript : MonoBehaviour {
public float speed;
public Transform targetWall;
public GameObject wall;
public GameObject raise;
// Use this for initialization
void Start () {
wall = GameObject.Find("verticalWall_1");
}
// Update is called once per frame
void Update () {
}
void OnTriggerEnter(Collider other)
{
if(other.gameObject.tag == "Player")
{
//open the wall
//raise = wall.GetComponent<gameObject>();
//raise.raiseWall();
//Debug.Log(wall.ToString());
Debug.Log("The button clicked, raising the wall");
}
}
/*
void raiseWall()
{
float step = speed * Time.deltaTime;
targetWall.transform.position = Vector3.MoveTowards (transform.position, targetWall.position, step);
}*/
}
and the raiseWall.cs script (placed on the wall)
using UnityEngine;
using System.Collections;
public class raiseWall : MonoBehaviour {
public float speed;
public Transform target;
public bool raised=false;
// Use this for initialization
void Start () {
//Vector3 target = new Vector3 (transform.position.x, transform.position.y + 3, transform.position.z);
}
// Update is called once per frame
void Update () {
if (raised == true)
{
float step = speed * Time.deltaTime;
//transform.Translate (0,Time.deltaTime,0,Space.World);
transform.position = Vector3.MoveTowards (transform.position, target.position, step);
}
}
public void raiseTheWall()
{
raised=true;
}
}
Answer by RazvanGabriel · Jan 28, 2015 at 07:58 PM
Ok so I got it. Now it works:
doorTriggerScript:
using UnityEngine;
using System.Collections;
public class doorTriggerScript : MonoBehaviour {
void Start () {
}
// Update is called once per frame
void Update () {
}
void OnTriggerEnter(Collider other)
{
if(other.gameObject.tag == "Player")
{
//this is all I need to call the method
GameObject go = GameObject.Find("verticalWall_1");
go.GetComponent<raiseWall> ().raiseTheWall ();
Debug.Log("The button clicked, raising the wall");
}
}
}
The other script (raiseWall) remains the same.
Here are more details: http://docs.unity3d.com/412/Documentation/ScriptReference/index.Accessing_Other_Game_Objects.html
You probably don't want to use the getcomponent or find code in your script when you go through the trigger. It's much better to use them on startup ins$$anonymous$$d. For an example of how to do that, see my below comment.
Answer by Superrodan · Jan 28, 2015 at 08:00 PM
The two scripts you have look a little redundant.
The first one is a trigger script specifically meant to raise a specific wall. It finds an object in the scene called "verticalWall_1" and raises that wall automatically when you go through the trigger.
The second script is a script that raises a wall when you tell it to. It doesn't have the trigger component so something would have to tell it to trigger.
If your question is how to make it so that the first script can raise any wall, then you would need to remove this code:
wall = GameObject.Find("verticalWall_1");
un-comment out this code:
//raise = wall.GetComponent<gameObject>();
//raise.raiseWall();
and un-comment out this code:
/*
void raiseWall()
{
float step = speed * Time.deltaTime;
targetWall.transform.position = Vector3.MoveTowards (transform.position, targetWall.position, step);
}*/
from the first script. Put that script on your trigger, and then just drag whatever you want to be raised into the "wall" slot that will show up in the inspector.
ALTERNATIVELY if yo really need to do it with two scripts for some reason with the setup you currently have, then you will need to use a getcomponent script. I'll go over that in a comment on this post because it's already getting long
Ok, so if you really need to use two scripts, then the easiest way I can see is in your first script to change this line:
public GameObject wall;
to:
public raiseWall wall;
In the start function of your first script you would change this:
wall = GameObject.Find("verticalWall_1");
to:
wall = GameObject.Find("verticalWall_1").GetComponent<raiseWall>();
Then, when you go through the trigger, in this code:
if(other.gameObject.tag == "Player")
{
//open the wall
//raise = wall.GetComponent<gameObject>();
//raise.raiseWall();
//Debug.Log(wall.ToString());
Debug.Log("The button clicked, raising the wall");
}
you would just need to call your "raiseTheWall" function ins$$anonymous$$d:
if(other.gameObject.tag == "Player")
{
wall.raiseTheWall();
}
When using only one script, that on the trigger, I seem to get 2 errors at:
raise = wall.GetComponent<gameObject>();
raise.raiseWall();
Assets/Scripts/doorTriggerScript.cs(30,51): error CS0246: The type or namespace name gameObject' could not be found. Are you missing a using directive or an assembly reference? ----------and Assets/Scripts/doorTriggerScript.cs(31,31): error CS1061: Type
UnityEngine.GameObject' does not contain a definition for raiseWall' and no extension method
raiseWall' of type `UnityEngine.GameObject' could be found (are you missing a using directive or an assembly reference?)