C#
Hi there! I’m having some trouble making one script communicate with the second one. Can somebody please check my work to see where I’m wrong? Here’s a short summary of what I’ve been trying to do so that you’ll have a clearer idea of my problem.
-
I have a character who can do a “Push” attack by pressing a key (I set it to letter “E”). This is in my PlayerControl script.
-
I created two empty gameobjects and gave them capsule colliders and then set them as triggers. I then attached them to the palms of my character. These gameobjects serve as the collision trigger so that when my character does the push attack, it would follow the palms and hit the enemy in front. These gameobjects were given the tag of “ChubbyPush” because enemies can die if they come into contact with something with this tag.
-
Although I already can use these two collision capsules to kill enemies, I realized one thing: Even if I wasn’t doing the “Push” attack, the enemies can die by simply touching my character’s palms! That’s not good because I only want them to die if my character attacks them.
-
After that, I decided to make a second script called ChubbyPushAttack, intended to change the tags of the collision capsules depending on whether I already pushed the “E” key or not. If I pushed the “E” key, then the tags on the collision capsules should change into “ChubbyPush”, if not, then they should remain as “Untagged”.
-
So far I’ve been testing it but it doesn’t seem to work. Even if I push the “E” key, the tags on the collision capsules don’t change into “ChubbyPush” (yes, I was looking at the inspector to see whether it was working or not). I must be doing something wrong when it comes to making one script communicate with the other.
Here’s my PlayerControl script, I’ll just include the relevant Push Attack part (I don’t want to drown you with too much unrelated stuff, it’s very long). I attached this to my cute character named Chubby:):
using UnityEngine;
using System.Collections;
public class PlayerControl : MonoBehaviour
{
private bool canMove = true;
public bool chubbyUsedPushAttack;
void FixedUpdate ()
{
if (canMove) {
CharacterIdle();
CharacterMove ();
CharacterJump ();
CharacterPush ();
}
else
if (!canMove) {
animator.SetBool("CanDie", true);
RestartCountdown ();
}
}
void CharacterPush()
{
if (Input.GetKey (KeyCode.E) animator.GetCurrentAnimatorStateInfo(0).IsName ("Idle") isGrounded == true
|| Input.GetKey(KeyCode.E) animator.GetCurrentAnimatorStateInfo(0).IsName("Walk") isGrounded == true)
{
chubbyUsedPushAttack = true;
print ("Chubby used Push Attack! " + chubbyUsedPushAttack);
animator.SetBool("CanPush", true);
rigidbody.velocity = transform.forward * 5;
}
}
}
Here’s the other script that’s supposed to change the tags of the collision capsules if I press the “E” key to push. Of course they are attached to the two collision capsules:
using UnityEngine;
using System.Collections;
public class ChubbyPushAttack : MonoBehaviour
{
public PlayerControl playerControlReference;
public bool chubbyPushed;
// Use this for initialization
void Start () {
chubbyPushed = playerControlReference.chubbyUsedPushAttack;
}
// Update is called once per frame
void Update () {
if (playerControlReference.chubbyUsedPushAttack == true) {
print ("Chubby Push Attack: Attacked!");
this.gameObject.tag = "ChubbyPush";
} else
if (playerControlReference.chubbyUsedPushAttack == false)
{
print ("Chubby Push Attack: Did Not Attack!");
this.gameObject.tag = "Untagged";
}
}
}
My goal is:
a. When I press the “E” key, the bool called chubbyUsedPushAttack should become true in my PlayerControl (This one works, I checked).
b. The ChubbyPushAttack script should be able to change the tags of my collider capsules from “Untagged” to “ChubbyAttack” if the bool chubbyUsedPushAttack is true in the PlayerControl script so that enemies can be killed if they come into contact with these capsules.
So there, my second script called ChubbyPushAttack doesn’t seem to work at all. I’m also pretty new to the idea of using references like “public PlayerControl playerControlReference;” because it’s the first time I needed to make two scripts interact with each other. I just followed some tutorial that I found regarding references, but unfortunately when I tried to apply it on my own, I seem to have failed.
Thanks in advance to whoever can give me advice on how to make my idea work. I also welcome tips on how to make different scripts interact with one another because I think this is a big part of making things work in games. It’s still something that I haven’t overcome. I’m just not too experienced when it comes to this matter. I really hope to learn using references more comfortably in the future, I think it will solve a lot of potential problems later on:)



