Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 De4dite · Sep 20, 2021 at 07:31 PM · positioning

Changing player position on start based on the position values of a randomly selected cube from a list

Hello,

I have a series of cubes placed in my level and I want to randomly select one of them, and transform the position of the player according to the position values on the selected "spawn" object, on start. I'm new to programming as a whole, so I'm not quite sure what detail I am missing for this to occur.

   public GameObject player;
   public GameObject[] spawnspots;
   private Vector3 spawnLocation;
   private Vector3 PlayerStart;
   void Awake()
   {
       player = GameObject.Find("Player");
       PlayerStart = player.transform.position;
       spawnspots = GameObject.FindGameObjectsWithTag("Respawn");
   }
   void Start()
   {
       setspawnlocation();
   }
   void setspawnlocation()
   {
       int spawn = Random.Range(0, spawnspots.Length);
       spawnLocation = spawnspots[spawn].transform.position;
       PlayerStart = spawnLocation;
   }
Comment
Add comment
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

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by bdubbert · Sep 20, 2021 at 08:01 PM

This doesn't look to bad, I think the only thing you are missing is to actually use your cube location to move the player to your desired spot. Add this to the end of your setspawnlocation function.

 player.transform.position = spawnLocation;

I think where you are getting mixed up is you think that by setting PlayerStart = player.transform.position; in your Awake method, you can now treat PlayerStart as player.transform.position, just under a different name. This is not the case. When you call PlayerStart = player.transform.position, PlayerStart just gets a copy of whatever value player.transform.position has. If the player position was (1,1,1), PlayerStart is now (1,1,1). But if the player position changes, PlayerStart keeps its value of (1,1,1).


So when you say PlayerStart = spawnLocation; you are not changing the position of the player, you are just saying "Ok, PlayerStart had some Vector3 value that was at one point copied from the player position, but now I want it to forget that value and copy from spawnLocation instead".


If this is still confusing I recommend looking up the difference between "pass by reference" and "pass by value". Since Vector3 is a struct it always copies its data, rather than holding a reference to the original data.

Comment
Add comment · Show 2 · 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 De4dite · Sep 20, 2021 at 11:54 PM 0
Share

Thank you for your response! I understand now what the error of my logic was! I set up some debugs after your suggested fix. It seems like the values are being assigned correctly, but for whatever reason my player prefab still won't budget. The player scripted used a private Transform that I made public, but that also hasn't done anything.

alt text

screenshot-2021-09-20-195259.jpg (17.6 kB)
avatar image bdubbert De4dite · Sep 21, 2021 at 03:07 PM 0
Share

Hmm yea something is off... are you sure that the "player" variable in your script is pointing at the correct object? It looks like your script is moving some transform, but not the one that you are expecting to move.

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

126 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

Related Questions

GUI changes when game runs full screen 2 Answers

placing children objects in different places 1 Answer

Animation from actual position instead of animation starting position 0 Answers

Gyroscope default position 2 Answers

Ideas on enemy unit formations solution 0 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