1
\$\begingroup\$

I wrote a simple script to attempt to do this and I created an empty game object and applied the script to it and dragged both of my sprites into my script and I receive no errors but nothing happens when my player enters the collider and the "Calm" version of the sprite is still active.

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;

public class JetAnglerSwap : MonoBehaviour {
public GameObject CalmAngler;
public GameObject AngryAngler;
// Use this for initialization
void Start () {

}

void OnTriggerEnter2d(Collider2D other)
{
    if (other.CompareTag("Player"))
    {
        SwapAnglers();
    }
}
    void SwapAnglers()
{
    CalmAngler.gameObject.SetActive(false);
    AngryAngler.gameObject.SetActive(true);
}
}
\$\endgroup\$
  • 1
    \$\begingroup\$ To diagnose this, we'll need to see how you've set up the two colliding objects, their colliders, tags, Rigidbody if applicable. \$\endgroup\$ – DMGregory Dec 10 '18 at 12:13
  • \$\begingroup\$ I got it figured out. I'm going to update my post but I'm not with my pc at the moment. \$\endgroup\$ – Mark Gregg Dec 10 '18 at 22:34
  • 1
    \$\begingroup\$ Just as an FYI, you're allowed to answer your own questions; so please post the solution as an answer, not as an edit, so people can easily tell the difference between problem and solution in the future. \$\endgroup\$ – Stephan Dec 11 '18 at 16:35
0
\$\begingroup\$

Well my workaround here was to create an empty game object called AggroArea and apply this script to it along with a box collider 2D of the size of my water area. When the player jumps in it immediately swaps from one of my public enemy sprites to the other one. It does this by counting the collision of the player as it makes contact with the collider of the water. I have the option to have it do something by writing something in the offstate function but as of now I don't need offstate to do anything.

public class JetAnglerSwap : MonoBehaviour {
int playerCount = 0;
public GameObject CalmAngler;
public GameObject AngryAngler;

void OnTriggerEnter2D(Collider2D other)
{
    if (other.tag == "Player")
    {
        playerCount++;
    }
    else
    {
        playerCount = 0;
    }
}

void Update()
{
    if (playerCount == 2)
    {
        // default on state
        OnState();
    }

    else
    {
        // default off state
        OffState();
    }
}

private void OnState()
{

    CalmAngler.gameObject.SetActive(false);
    AngryAngler.gameObject.SetActive(true);
}
   private void OffState()
  {

  }
}
| improve this answer | |
\$\endgroup\$
  • \$\begingroup\$ You might not want to use the Update function for this though; You could just check the value of playercount and call OnState() or OffState() accordingly ;) \$\endgroup\$ – user115399 Dec 13 '18 at 6:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.