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 Alienjesus · Mar 22, 2011 at 08:08 PM · tagsfunctionsdeath

Respawning objects upon player death problems

Ok, so I'm making a puzzle game where you move the objects in the environment about to progress. Because they're so important, I need to make sure they aren't permanently missable. So I added code to make it that if an object drops off the edge of the screen, they respawn where they first started the level.

However, I also wanted to make it so that when the player died, all the moved objects would respawn in their starting locations. However, the codee I've written to do so crashes Unity everytime it's activated. I think it might be due to the workload required from the code (which is a real problem for my project if so, I'm a bit worried), but just in case it's a coding issue, I thought I'd post this here. Perhaps my code is causing it?

Anyway, here is the code. Upon falling into the pit, an OnDeath message is sent to the main character.

This activates this code on the characters, which searches for moveable game objects and sends a death message out to them too.

 function OnDeath () {
    var moveables : GameObject[];
    moveables = GameObject.FindGameObjectsWithTag("Moveable");
//Moveable objects are all tagged as such, this script searches for them.
    for (var moveable in moveables)
    {SendMessage ("OnDeath", SendMessageOptions.DontRequireReceiver);
    }
        Spawn ();
//This line respawns the character themselves
    }

In turn, when the objects recieve this message, this script is activated.

private var spawnPos : Vector3; private var spawnRot : Quaternion;

private function Awake() { spawnPos = transform.position; spawnRot = transform.rotation; //Debug.Log (spawnPos); //Debug.Log (spawnRot); }

function OnDeath () { transform.position = spawnPos; transform.rotation = spawnRot; }

At the moment, there are about 20 moveable objects in my scene, but in my final game, this is liable to become a lot more. Am I going to be in trouble here?

P.S. it'd definetely one of these sections of code causing the crashes, it happens literally the second you die.

Comment
Add comment · Show 3
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 Joshua · Mar 22, 2011 at 08:41 PM 0
Share

I at the moment have no time to really look into your code and track down the problem - I will later if it's not fixed yet by then. But just a little suggestion: Can't you just reload the scene upon player death? That would make the workload a lot smaller. It's perhaps not the nicest workaround, but by putting DontDestroyOnLoad on a few things you could probably make it work, if I understand the way your game works correctly.

avatar image Joshua · Mar 22, 2011 at 08:42 PM 0
Share

This would of course imply that the 'moving stuff part' of your game takes place in a seperate scene, but by loading that scene upon entering a hallway for instance, it should be (I think) possible.

avatar image Alienjesus · Mar 22, 2011 at 09:05 PM 0
Share

The moving stuff scene is my only scene currently, but reloading the scene sounds like it could be an option. The only problem I foresee is that I want the player to respawn at the last checkpoint, not the beginning of the scene each time. It sounds like this 'DontDestroyOnLoad might work, what exactly does it do?

2 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by Alienjesus · Apr 24, 2011 at 04:21 PM

Decided to reload the scene upon death, looking into using singletons to save checkpoints.

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 loramaru · May 04, 2011 at 04:08 AM

I believe your problem is an infinite recursion on the player's OnDeath handler. By calling SendMessage() without an explicit object, you are calling it on the current object, the player. Instead, you want...

for (var moveable in moveables)
{
    moveable.SendMessage("OnDeath", SendMessageOptions.DontRequireReceiver);
}
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

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

No one has followed this question yet.

Related Questions

Two cameras and switching 2 Answers

Webplayer templates custom tags (and the player settings GUI) 0 Answers

Have something never lose momentum? 1 Answer

Trigger not registering imported objects 2 Answers

how many tags is too many 3 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