- Home /
horror game script ring and bell and the ghost disappears temporally
Trying to make a script that when you ring the bell the ghost disappears for a certain amount of time then reappear
I know how to yield return new WaitForSeconds and triggering another script but not sure how to combined them, this what I came up with but not sure what is exactly wrong us ``uing System.Collections; using System.Collections.Generic; using UnityEngine;
public class Ringbell : MonoBehaviour
{
public bool IsRing = false;
public bool Death = false;
public GameObject bell;
public AudioSource bellsring;
public GameObject Ghost;
void Update()
{
if (Input.GetMouseButton(0))
{
if ((IsRing == false))
{
StartCoroutine(ringthebell());
}
}
}
IEnumerator ringthebell()
{
bell.SetActive(true);
IsRing = true;
bell.GetComponent<Animator>().Play("ringbell");
bellsring.Play();
yield return new WaitForSeconds(1f);
bell.GetComponent<Animator>().Play("none");
IsRing = false;
bell.SetActive(false);
disappear Disappear = ringthebell.transform.GetComponent<disappear>();
if (target != null)
{
Disappear.bellrung();
}
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor.Animations;
public class disappear : MonoBehaviour
{
public GameObject Ghost;
public Animator controller;
public void bellrung()
{
StartCoroutine(bellrung());
}
IEnumerator bellrung()
{
controller.SetBool("Death", true);
yield return new WaitForSeconds(0.05f);
Ghost = false;
yield return new WaitForSeconds(5f);
Ghost = true;
}
}
@jackmw94 That helped, also I talk to a friend that toke coding back in college and said I was missing a trigger for the ringbell script to the disappear script, I need like a raycast hit or something to that effect, but I'm not sure how to do it, cause I want it to be a area of effect thing, any suggestions
So does your ghost disappear now? Yeah you are triggering the bell ring with a mouse button click. How would you like to trigger the bell? On collision with the player? With mouse click on the bell? Btw is your game 2D or 3D?
@MarekRimal no, the part that is only ringing the bell works.
Yes this is 3D So the character carries the bell the entire game so I was thinking, the trigger should spore from the character, on collusion with a sphere collider would be fine, but the character should be able to rung said bell weather the ghost is in collision or not but if the ghost it should make the ghost trigger death animations and disappear temporarily
@MarekRimal I have the mouse click on the character references to the Bell by the public GameObject bell;
I have decided to reconstruct your code. When you are programming its a good idea to model the entities in the game as classes. Then you can attach each class to the game entity. That means the Bell can be one class with all its functionality, the Ghost can be other class and so on. Try to have a look at this code. I didnt run it but it should be correct.
public class Bell : MonoBehaviour
{
public bool isRinging = false;
public GameObject bellObject; // This is the object you want to hide - probably a child of the object with this script
public Ghost ghost;
public AudioSource audioSource;
private void Update()
{
if (Input.GetMouseButton(0))
{
if (isRinging == false)
{
StartCoroutine(Ring());
}
}
}
private IEnumerator Ring()
{
bellObject.SetActive(true);
isRinging = true;
bell.GetComponent<Animator>().Play("ringbell");
audioSource.PlayOneShot();
yield return new WaitForSeconds(1f);
bell.GetComponent<Animator>().Play("none");
isRinging = false;
bellObject.SetActive(false);
if (ghost != null)
{
ghost.Disappear();
}
}
// This is how you can trigger the bell on collision (on trigger)
private void OnTriggerEnter(Collider other)
{
// Check if the collision is the player - it has to have Player script attached
if(other.gameObject.GetComponentInParent<Player>() != null && isRinging == false)
{
StartCoroutine(Ring());
}
}
}
public class Ghost
{
public Animator animator;
public GameObject ghostBody; // This is the body of the ghost which you want to hide - probably a child of the object with this script
public IEnumerator Disappear()
{
animator.SetBool("Death", true);
yield return new WaitForSeconds(0.05f);
ghostBody.SetActive(false);
yield return new WaitForSeconds(5f);
ghostBody.SetActive(true);
}
}
Answer by MarekRimal · Apr 08 at 02:09 PM
What does Ghost = false
do? Ghost is the GameObject you want to hide I guess. Then use Ghost.SetActive(false)
Btw I really really recommend to use some consistent naming for variables, methods and classes. It makes the code much easier to read. Google some naming convention (idealy C#) and look at it. For class names is usually used PascalCase and for variable names camelCase. I personally use uderscore for class variable names like this: _variable. The main point is that is has to be consistent.
Answer by jackmw94 · Apr 07 at 11:50 PM
When you say you’re not sure what is wrong, what is happening?
I can see from your code you’ll have a compiler error in bellrung in your disappear class. Your variable Ghost is a GameObject but you’re assigning a bool value to it (true/false). Also in ringthebell in your Ringbell class you have an undefined variable called target.
If you want to set the ghost inactive then you can call SetActive(false) like you do with the bell GameObject. I am not sure what target should be or whether you need it.
If this doesn’t solve your problem then let me know what you expect to happen compared to what is actually happening :)
Hi @Rayith
If you don't have it already, I'd say you should keep the console window open at all times when programming! Any errors that don't go away once you press clear are compiler errors, which mean that your code is not compiling and therefore when you press play your code will not have updated since the last time it successfully compiled. An IDE will tell you whether you have compiler errors too (e.g. visual studio, rider)
I'm not completely sure I understand your intended behaviour, however you can make use of the builtin monobehaviour function 'OnMouseDown' to avoid having to manage the raycasting yourself. The game object that your Ringbell script is attached to has to have a collider on it (with IsTrigger set to false) then the OnMouseDown function will fire automatically when you click on that object. So you could replace your Update function with:
private void OnMouseDown()
{
if ((IsRing == false))
{
StartCoroutine(ringthebell());
}
}
If you're still having issues then let me know a little more about what's happening, whether it's compiler errors or just things not happening when you play