Difficulty setting script on gameObject from another script.
Greetings all
I have a bug in my game that I hope I can be assisted with.
I have a game object ( my player) and an objects that supposed to stun my player when they collide.( cant move or job) Please see my script below.
public class FallingObjectControl : MonoBehaviour
{
//Get the Character
public GameObject playerObject;
public Rigidbody2D _rigidbody;
private bool Stunned;
public float StunnedFor = 5.0f;
private CharacterHorizontalMovement mainChar;
private bool AbilityPerScript;
// Use this for initialization
void Start()
{
}
void FixedUpdate()
{
if (Stunned)
{
Debug.Log("Stunned");
// HandleStun();
StartCoroutine(HandleStun());
}
}
// Update is called once per frame
void Update()
{
}
private IEnumerator HandleStun()
{
//Stunned, De activate movement
playerObject.GetComponent<CharacterHorizontalMovement>().AbilityPermitted = false;
playerObject.GetComponent<CharacterJump>().AbilityPermitted = false;
// CharacterHorizontalMovement moveScript = GetComponent<CharacterHorizontalMovement>();
// CharacterJump jumpScript = GetComponent<CharacterJump>();
// jumpScript.AbilityPermitted = false;
// moveScript.AbilityPermitted = false;
// _rigidbody.velocity = new Vector3(0, 0, 0);
// Vector3 vel = _rigidbody.velocity;
// vel.z = 0f;
// _rigidbody.velocity = vel;
// GameObject.Find("spine-space-cat").SetActive(false);
// this.gameObject.GetComponent<BoxCollider2D>().enabled = (false);
// GameObject.Find("spine-space-cat");
// if (GameObject.GetComponent <CharacterHorizontalMovement>().WalkSpeed)
// if (GameObject.Find("PlayerSpaceship").GetComponent<SpaceshipMovement>().dead == true)
//Wait for 5 seconds then activate controls
yield return new WaitForSeconds(StunnedFor);
playerObject.GetComponent<CharacterHorizontalMovement>().AbilityPermitted = true;
playerObject.GetComponent<CharacterJump>().AbilityPermitted = true;
Stunned = false;
Debug.Log("Start Moving");
// jumpScript.AbilityPermitted = true;
// moveScript.AbilityPermitted = true;
Stunned = false;
// moveScript.AbilityPermitted = true;
// GameObject.Find("Arrows").SetActive(true);
// GameObject.Find("ButtonA").SetActive(true);
}
void OnTriggerEnter2D(Collider2D col)
{
if (col.gameObject.name == "spine-space-cat")
{
Debug.Log("Collided with Character FallingObjectcontrol");
Stunned = true;
}
}
}
Now when the item touches my character, the debug log shows as it should, however stunned wasnt getting set to true in the inspector so I changed it to private. Nevertheless, I believe the fixed update is called because "Stunned" is shown in the log. I then attempt to call the coroutine to stun my player for 5 seconds. CharacterHorizontalMovement and CharacterJump is both a script on my player, however, I am unable to set their ability permitted to false even if my code flow seems correct. Interestingly, when I stop my game AbilityPermitted for Characterjump is most times disabled.
So I unsure what I am doing wrong, appreciate if anyone could assist.