Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Calling scripts from other gameObjects??

Discussion in 'Scripting' started by Lmaryon, Jul 22, 2014.

  1. Lmaryon

    Lmaryon

    Joined:
    Jul 15, 2014
    Posts:
    18
    Hi, I have a collision script on my Player object that detects when it collides with my other Obstacle object. They are both sitting on top of my moving Platform object. What I need to do is to stop the Platform's Vector3.left method when Player and Obstacle collision is detected. What s the best way to do this??? None of the objects are triggers.
     
  2. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,773
    There are a couple of ways to do this, each depending on how exactly the logic ought to work.

    Most likely: is your "obstacle" object attached to the platform (e.g. it's a switch that works on that platform only)? Put a reference to the platform on the obstacle.
    Code (csharp):
    1.  
    2. //Obstacle.cs
    3. public class Obstacle : MonoBehaviour {
    4. public GameObject myPlatform;
    5. }
    6.  
    Now, look at the obstacle in the inspector - you'll see a "slot" into which you can drag your Platform GameObject. From there, you can use myPlatform in any function on the obstacle script.
     
  3. Lmaryon

    Lmaryon

    Joined:
    Jul 15, 2014
    Posts:
    18
    Ok, I've done that. So how can I stop the platform movement when I get a collision? Or just stop the Platform script from running all together?
     
  4. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,773
    Use OnCollisionEnter; if you use col.gameObject.GetComponent<Obstacle>(), you can access scripts on the object you collided with.
     
  5. Lmaryon

    Lmaryon

    Joined:
    Jul 15, 2014
    Posts:
    18
    What if you want to access a script on an unrelated object? And then stop a method within that script?
     
  6. LightStriker

    LightStriker

    Joined:
    Aug 3, 2013
    Posts:
    2,717
    How unrelated?
     
  7. Lmaryon

    Lmaryon

    Joined:
    Jul 15, 2014
    Posts:
    18
    Sorry I mean, how do I stop a script in the object I have put in the slot?.. lol sorry if this is confusing for you guys
     
  8. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    pseudo code
    Code (csharp):
    1.  
    2. class Platform
    3. {
    4. public boolean stopped = false; --or private is you are using the stopfunction approach
    5.  
    6. void Update()
    7. {
    8.     if(!stopped)
    9.     {
    10.         <movementcode>
    11.     }
    12. }
    13.  
    14. void StopPlatform()
    15. {
    16.     stopped = true;
    17.     <other stuff that happens when the platform stops>
    18. }
    19. }
    20.  
    then to use
    Code (csharp):
    1.  
    2. myPlatform.StopPlatform()
    3. or
    4. myPlatform.stopped = true
    5.  
    allowing other object to access variables without functions is a bit of a debate; you don't have to have the StopPlatform() function, but I think it's preferable since you can handle effects, sounds etc. in there when the platform is stopped.
     
    Lmaryon likes this.
  9. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
  10. Lmaryon

    Lmaryon

    Joined:
    Jul 15, 2014
    Posts:
    18
    oOk think I got it. Thanks guys! No doubt I'll be back ..