- Home /
Calling 2 objects into script for onCollisionEnter?
Basically have a moving platform that is working great, and now I am trying to get a trigger going so that when the player enters it then the platform starts its animation of going back and forth as it does. I want to be able to do this through the platform script to keep things more modular, but cannot figure out how the right way to use the two objects of player and P3Trig in conjuction with OnTriggerEnter2D. The following is my failed attempt:
using UnityEngine;
using System.Collections;
public class Pmove3 : MonoBehaviour {
public GameObject Player;
public GameObject P3Trig;
if (Player){
void OnTriggerEnter2D(Collider2D col)
{
if(col.gameObject.tag == "P3Trig")
{
moveStart = true;
Pmove3Control ("Start");
}
}
Thanks a million! Look forward to paying the help forward here.
So the trigger is a seperate component on the same object as the collider?
Answer by RayJr · Nov 19, 2014 at 06:17 PM
If the answer to the above comment is true, you can get the 'mover' script like so - replace "Mover" with the name of the script doing the moving.
Mover moveScript = GetComponent.<Mover> ();
If the script is on a different object then the trigger, you'll need to get the platform object first. you're better off putting the trigger on the same object though, so you don't need to specify the platform.
gameObject oPlatform = Find.GameObject ("Platform");
Mover moveScript = oPlatform.GetComponent.<Mover> ();
with that you can set a variable or call a function on that script like so
moveScript.Move = true;
or
moveScript.StartMove();
just make sure the function and variables are set to public. Also, i haven't compiled this, so you might need to modify it a bit to work
I think we had some misunderstandings....Here is the entire code:
using UnityEngine;
using System.Collections;
public class Pmove3 : $$anonymous$$onoBehaviour {
public GameObject Player;
public GameObject P3Trig;
Animator animator;
bool moveStart;
void Start ()
{
moveStart = false;
animator = GetComponent<Animator>();
}
void OnTriggerEnter2D(Collider2D col)
{
if(col.gameObject.tag == "P3Trig")
{
moveStart = true;
Pmove3Control ("Start");
}
}
void OnTriggerExit2D(Collider2D col)
{
if(moveStart)
{
moveStart = false;
Pmove3Control ("Return");
}
}
void Pmove3Control(string direction)
{
animator.SetTrigger(direction);
}
}
This IS the platform move code, and it is attached to the platform object. By 'mover,' do you mean player? I wanted to have all this script contained inside the platform object so I don't have to write more script into the main players script to correspond with every new trigger for every new platform. I figure that if the platform script can call both the player, who is to trigger the trigger, and the trigger- then I can make as many moving, trigger-able platforms as I want without having to further modify the player script. I called them both in, I know how to do that, but I do not know how to tie the OnTriggerEnter2D to the Player to complete the script. Just the lines with the OnTriggerEnter2D if collide with P3 Trig needs to be fixed to something like:
void --->PLAYER<---OnTriggerEnter2D(Collider2D col) { if(col.gameObject.tag == "P3Trig") {
So that player is somehow in that line. I appreciate the help soooo much in advance thank you.
If this is on the platform, then 'col' will be the player once he lands on it, no?
if you take your Enter collider function and temporarily update it to :
void OnTriggerEnter2D(Collider2D col)
{ Debug.Log (col.gameOjbect.name) }
You should see the name of your player. is your player have it's tag set to P3Trig? That might be the problem. Name and Tag are different things.
I'm not at my development computer right now, I don't have unity in front of me. I'll take a look later when i'm home if its not solved by then.
No- Player, is the player, tagged as Player. P3Trig is the trigger, named and tagged P3Trig. They are both being called into a script which is on the platform.
public GameObject Player; public GameObject P3Trig;
Player needs to trigger the trigger (P3Trig). Can this even be done? I'm getting a feeling that maybe not?
Yes it can. In your OnTriggerEnter2D , 'col' will be the player object (or anything else that touches it). It will never be itself ( P3Trig). So your if statement will not run. Update the if statement to check for the player object's tag.
On void OnTriggerExit2D, update that if statement as well to check for the player's tag. If it is true, that means the player is leaving the platform. Don't bother checking if $$anonymous$$oveStart is set to true, if the player is leaving the platform, it will be.
try this..
using UnityEngine;
using System.Collections;
public class Pmove3 : $$anonymous$$onoBehaviour {
public GameObject Player;
public GameObject P3Trig;
Animator animator;
bool moveStart;
void Start ()
{
moveStart = false;
animator = GetComponent<Animator>();
}
void OnTriggerEnter2D(Collider2D col)
{
if(col.name == "Player")
{
moveStart = true;
Pmove3Control ("Start");
}
}
void OnTriggerExit2D(Collider2D col)
{
if(col.name == "Player")
{
moveStart = false;
Pmove3Control ("Return");
}
}
void Pmove3Control(string direction)
{
animator.SetTrigger(direction);
}
}
Stiiiill appear to not be understanding each other I think sorry...The script has nothing to do with the player being on the platform. It should do the following: 1. Player enters P3Trig 2. Platform starts it's animation of moving back and forth.
It looks like you just replaced
if(col.name =="P3Trig")
with
if(col.name =="Player")
So now, correct me if I am mistaken, there is no longer any reference to the trigger, P3Trig????