Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by rayith · Apr 07 at 10:39 PM · waitforseconds

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;
     }
 }
Comment
Add comment · Show 5
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image rayith · Apr 08 at 01:20 PM 0
Share

@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

avatar image MarekRimal · Apr 08 at 03:44 PM 0
Share

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?

avatar image rayith · Apr 08 at 04:01 PM 0
Share

@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

avatar image rayith · Apr 08 at 04:04 PM 0
Share

@MarekRimal I have the mouse click on the character references to the Bell by the public GameObject bell;

avatar image MarekRimal rayith · Apr 08 at 04:23 PM 0
Share

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);
     }
  }

2 Replies

· Add your reply
  • Sort: 
avatar image
0

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

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 :)

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image jackmw94 · Apr 08 at 04:10 PM 0
Share

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

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

138 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Trying to implement synchronized laser system. 1 Answer

Trying to create two consecutive time delays 0 Answers

How does WaitForSeconds pass values? 0 Answers

yield in C# doesn't work, not event the sample code? 1 Answer

Wait For Seconds to Load level C# 2 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges