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
1
Question by 2Tie · Mar 28, 2011 at 09:31 PM · findrespawn

Finding object via script

In one of my scripts, I am trying to get an object to respawn when it collides with my death boundary. The collision works, and the character loves a life, but it won't move to it's spawnpoint. I should probably show snippets of my code so you guys can understand better:

var spawnpointobject:GameObject;
var spawnpoint:Transform;
function Start() {
gameObject.tag = "Player";
transform.position=spawnpoint.transform.position;
spawnpointobject = gameObject.Find("P1Spawn");
spawnpoint = spawnpointobject.transform;
}
function OnDeath () {
stock--;
transform.position = spawnpoint.position;
}

When my character collides with my death boundary, a stock is removed, So I know that it's being called. But it keeps falling. I put in a Debug.Log(spawnpointobject) in the Update function, and it always returns null. I realise, I'm probably over complicating things, but I'm just trying to get it to work. Oh, and my spawn object is called P1Spawn, and it's at the regular level in the Hierarchy along with the character, when it's created.

Comment
Add comment · Show 1
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 Statement · Mar 28, 2011 at 09:46 PM 1
Share

"And the character loves a life" - That has to be the most uplifting thing I read today :) I love life too!

1 Reply

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

Answer by Statement · Mar 28, 2011 at 09:51 PM

This line of code...

transform.position = spawnpoint.transform.position;

... might throw an exception (if spawnpoint is null to start with). That means that any following code in that method will not be run, so it will never even call Find. Also...

spawnpointobject = gameObject.Find("P1Spawn"); 

... should be:

spawnpointobject = GameObject.Find("P1Spawn"); 

Also make sure you ain't creating P1Spawn in any other Start method because otherwise it might be created after you try to find it. However you could probably make things much much simpler like this:

function OnDeath () {
    stock--;
    transform.position = GameObject.Find("P1Spawn").transform.position;
}

It's not likely death happens every frame so it's ok to look for the spawn once it is called.

If it still doesn't work, make sure your objects name really is P1Spawn and that it is active. Otherwise it won't be found.

Imo, your script could be rewritten as:

gameObject.tag = "Player"; Respawn();

function OnDeath () { stock--; Respawn(); }

function Respawn() { transform.position = GameObject.Find("P1Spawn").transform.position; }

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 2Tie · Mar 28, 2011 at 10:06 PM 0
Share

Thanks a lot! It worked!

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

Make Deaths end game 1 Answer

Whats Wrong with my code? 1 Answer

How to make an enemy respawn? 0 Answers

Respawning script question. 1 Answer

How do I make a respawn system? 1 Answer


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