Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by RazvanGabriel · Jan 28, 2015 at 07:10 PM · collidertransformtriggerwall

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;
     }
 }
 
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
0

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

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Superrodan · Jan 28, 2015 at 08:07 PM 0
Share

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.

avatar image
0

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

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Superrodan · Jan 28, 2015 at 08:06 PM 0
Share

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();
 }
avatar image RazvanGabriel · Jan 28, 2015 at 08:56 PM 0
Share

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?)

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

2 People are following this question.

avatar image avatar image

Related Questions

Get the colliders transform (?) 1 Answer

Check if another object exist in a position before respawning 1 Answer

Destory/Collect object to open door 1 Answer

After building move doesnt move 1 Answer

Objects continue movement despite being triggered to stop 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges