Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 robbos729 · Jul 12, 2019 at 12:16 PM · 2d2d array

Co ordinates issue

I've set my co ordinates in my code for a unity 2d game to move the tile to a set position. However it never goes to the X or y I have defined in inspector leaving me a bit confused to what's wrong is it a simple fix or resolution issue. I do not have z defined also as I don't see the point

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 I_Am_Err00r · Jul 11, 2019 at 09:38 PM 0
Share

Yeah, without the code you use to move your character, couldn't begin to help you; please provide complete script that handles the movement.

avatar image robbos729 I_Am_Err00r · Jul 11, 2019 at 10:05 PM 0
Share
 using System.Collections;
 using System.Collections.Generic
 using Unity Engine;
 
 public class RandomPosition : $$anonymous$$onoBehaviour
 {
 public Vector3] positions;
 public void SetRandomPosition)
 {
 int random Number = Random.Range(0,
 positions.Length);
 transform.position positions [random Number];
 }
 }

This is a 2d game btw

avatar image Bunny83 robbos729 · Jul 12, 2019 at 01:02 PM 0
Share

Your code has countless of typing errors so it's impossible to find any actual error here. This code will not compile. Also we don't know if and where you might call that SetRandomPosition function. Provide your actual code. Also what did you already do to debug your issue? Have you checked that your code actually executes? Try adding a Debug.Log to see if the code actually runs.

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by I_Am_Err00r · Jul 12, 2019 at 12:44 PM

You defined positions as a Vector3, not an int. I hope this analogy doesn't offend you but you are trying to compare things that don't compare with that statement like this pen is a car. Try this:

  using System.Collections;
  using System.Collections.Generic
  using Unity Engine;
  
  public class RandomPosition : MonoBehaviour
  {
 float randomRangeX;
 float randomRangeY;
  public int randomRange;
  public Vector3 positions;
  public void SetRandomPosition)
  {
  randomRangeX = Random.Range(0,
  randomRange);
  randomRangeY = Random.Range(0,
  randomRange);
  positions = new Vector3 (randomRangeX, randomRangeY,  0);
  transform.position =  positions;
  }
  }

What I am doing here is assigning the values of x and y to floats (ints with decimal values) and then when you call the function, getting those random numbers generated. After you have those, when you define the Vector3 positions, you plug in the random x and y values you just created (and left z at 0 because you don't care about that in 2D). JUST A REMINDER, you need to set the value randomRange in the inspector for this to work.

Let me know if that works.

Comment
Add comment · Show 3 · 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 Bunny83 · Jul 12, 2019 at 01:20 PM 0
Share

Sorry but your code also makes not much sense just as the OP's code in the comment above. This won't compile. Also he set a list of positions in the inspector from which he want to choose from.

avatar image I_Am_Err00r Bunny83 · Jul 12, 2019 at 02:17 PM 0
Share

You're right, my code didn't compile (wrote it directly on this forum, not an IDE, didn't notice the errors I made when I originally wrote that, fixed it) but someone of your reputation should understand where I was going with that and where the OP is going as well, no need to be so abrasive.

Here is what I think OP is trying to do now that I gave this a second look:

     using System.Collections;
      using System.Collections.Generic
      using Unity Engine;
      
      public class RandomPosition : $$anonymous$$onoBehaviour
      {
      public Vector3[] positions;
      public void SetRandomPosition
      {
      int random Number = $$anonymous$$athf.RoundToInt(Random.Range(0,
      positions.Length)); // I think that is how you round a float to an int, I'm not using an IDE so I double check if you copy and past this
      transform.position = positions[random Number];
      }
      }
avatar image robbos729 · Jul 13, 2019 at 11:44 PM 0
Share

It gives random positions but I can't seem to customize where it goes so sometimes it goes a little of. Is there a way where I can say it can go from x100 y0 to x500 y100 so it can move anywhere out of them co ordinates

avatar image
0

Answer by robbos729 · Jul 13, 2019 at 06:09 PM

 For some reason it still goes to some which aren't desired is It the vector 3
 
 using System.Collections;
  using System.Collections.Generic;
  using Unity Engine;
       
       public class RandomPosition : MonoBehaviour
       {
       public Vector3[] positions;
       public void SetRandomPosition()
       {
       int randomNumber = Mathf.RoundToInt(Random.Range(0,
       positions.Length)); // I think that is how you round a float to an int, I'm not using an IDE so I double check if you copy and past this
       transform.position = positions[randomNumber];
       }
       }
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

215 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 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

2D Hex/ Square Tile Resource Controller Help 0 Answers

2d Array persists during multiple game sessions 2 Answers

Assets/Scripts/PlayerController.cs(32,49): error CS0126: An object of a type convertible to `float' is required for the return statement 1 Answer

How do I create a 2d shooting mechanic (Left and Right)? 1 Answer

2D array generated with custom inspector, null when starting game? 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