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 gradysocool · Nov 22, 2013 at 07:18 AM · c#error messagespawn points

How to move one object's position to another in unity? (c#)

How do you move one object's position to another in unity? I need to make my player object move to the spawn point when he dies. Here is the code I am using:

this.transform.position = spawnpointsscript.gameObject.transform.position;

I am having an error with this because I am putting this on a spawn point prefab, because I have multiple levels and want to be able to easily make copies of it. The error says:

An object reference is required to access non-static member `UnityEngine.Component.gameObject'

I am guessing this is because I put it on the prefab instead of the spawn point itself. Is there a way to do this without having to put the script on every individual object, or is that the only way?

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 gajdot · Nov 22, 2013 at 07:37 AM 0
Share

If you have a single spawnpoint in the scene, what you could do is add a tag for your spawnpoint like SpawnPoint and find that object in your script like this:

 GameObject spawnpoint = GameObject.FindGameObjectsWithTag("SpawnPoint"); 

and then use

 this.transform.position = spawnpoint.transform.position;

this script needs to be on your player.

avatar image gradysocool · Nov 23, 2013 at 03:25 AM 0
Share

Thank you for the answer it seems like it would work, but 2 error messages come up :

You are not allowed to call this function when declaring a variable. $$anonymous$$ove it to the line after without a variable declaration. If you are using C# don't use this function in the constructor or field initializers, Ins$$anonymous$$d move initialization to the Awake or Start function.

Cannot implicitly convert type UnityEngine.GameObject[]' to UnityEngine.GameObject'

avatar image Arreol · Aug 13, 2015 at 06:22 AM 0
Share
 GameObject spawnpoint = GameObject.FindGameObjectsWithTag("Spawnpoint");

Should be

     GameObject spawnpoint = GameObject.FindGameObjectWithTag("Spawnpoint");

2 Replies

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

Answer by YoungDeveloper · Nov 22, 2013 at 08:10 AM

This have been asked hundred times.. Check out related questions i already gave answers to:

http://answers.unity3d.com/questions/534933/random-spawn-random-prefab.html

http://answers.unity3d.com/questions/553385/enemy-spawning-system-in-c.html

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 gradysocool · Nov 23, 2013 at 02:54 AM 0
Share

I tried both of the links but the question they're asking is how to spawn (as in create) something in at the spawn point. I just want to make the player jump to the position of the spawn point. (Actually 5 above it but I know how to easily do that). The second one almost worked, but I cannot destroy and create the player (That would create problems) and recreate it using instantiate, I just want to move it. I had the game set up where it jumped to the position (0,5,0) but that means I would have to have the entire level around the (0,0,0). I just wanted to swap out (0,5,0) to the position of the spawn point (where ever I have it) + 5 in y, without messing up any of the scripts. Thank you for answering, though.

Edit: Oh, sorry. I didn't see someone answered it as a comment in my question.

Edit 2: Actually I thought about it and found the best way to do this would actually be to restart the level, and now it works fine. I'll mark the question as answered even though I didn't find the answer I was looking for.

avatar image
0

Answer by the1337est · Aug 13, 2015 at 07:58 AM

Create an empty GameObject named SpawnPoint and place it where you want your spawn point to be. Then just move the player to SpawnPoint's transform.position.

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

20 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

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

How can throw "Missing reference exception" this gameObject?.Getcomponent<>() ?? 2 Answers

How to unfreeze a Script in a unity5 Scene? 1 Answer

Upgrade C# language version to 7.0 Mac in Unity?,Upgrade project to C# language version '7.0' on a Mac? 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