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 deadfire56 · Jun 03, 2012 at 12:09 AM · objectvector3functionstring

Create a Custom Vector3 Object

Hi

I'm looking to create my own object that extends Vector3 that will allow me to have three x,y, and z parameters (string, string, and int).

I have no idea about how to go about this. I don't need any functions on them, just be able to create an object with those three parameters.

Comment
Add comment · Show 11
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 bompi88 · Jun 03, 2012 at 12:33 AM 0
Share

Vector3 is a sealed class that can't be extended, but why not make your own from scratch? I can't really get what you are trying to do. Can you explain what kind of information the object will contain? You have to have some "variable names" for your string-variables etc. Like x,y,z you have to have something like: xstring1, xstring2, ystring1, ystring2, zstring1, zstring2. OR do you want a Vector3 similar object that can take x,y,z variables of both string, float and int?

avatar image deadfire56 · Jun 03, 2012 at 12:48 AM 0
Share

Thanks for helping, I'm making a highscore board and when my game ends, it passes an object (I was trying to do vector3) with the time (XX:XX) the date (XX/XX) and the score(integer). Then my highscore script sorts a Vector3 array by the z parameter. So how would I make my own VectorCustom object?

avatar image Eric5h5 · Jun 03, 2012 at 03:34 AM 1
Share

That's nothing to do with a Vector3...a Vector3 is specifically 3 floats representing a vector. You're after a custom class that happens to have 3 elements, but they aren't x, y or z and shouldn't be referred to as such. You don't need to extend anything, just make a class as usual.

avatar image Fattie · Jun 03, 2012 at 02:40 PM 0
Share

Is it really worth spending so much time answering this question? :-)

avatar image Bunny83 · Jun 06, 2012 at 10:55 AM 0
Share

@Fattie: I would say: if there's one person that got confused about what a Vector3 is, then there will be another one ;)

Show more comments

2 Replies

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

Answer by Bunny83 · Jun 03, 2012 at 11:19 AM

If you want to be able to create a Vector3 from two strings and one int / float, you can create a function that creates one:

 //C#
 public static Vector3 CreateVector3(string aX, string aY, float aZ)
 {
     return new Vector3(float.Parse(aX),float.Parse(aY),aZ);
 }
 
 //UnityScript
 public static function CreateVector3(aX : String, aY : String, aZ : float) : Vector3
 {
     return Vector3(float.Parse(aX),float.Parse(aY),aZ);
 }

Note that those function don't do any error checks. If one of the strings doesn't contain a number that can be parsed into a float it will throw an exception.

This functions can be used like this:

 //C#
 Vector3 V = CreateVector3("12.5","10",5.0f);
 
 //UnityScript
 var V = CreateVector3("12.5","10",5.0);

If you want to store two strings and one int, like the others said, you have to create your own class / struct.

 //C#
 public struct MyDataType
 {
     public string s1;
     public string s2;
     public int number;
     public MyDataType(string aS1, string aS2, int aNumber)
     {
         s1 = aS1;
         s2 = aS2
         number = aNumber;
     }
 }

Don't ask me for the correct syntax in UnityScript since there is no direct way to create a struct. You have to derive a class from System.ValueType. Something like this:

 public class MyDataType extends System.ValueType
 {
     var s1 : String;
     var s2 : String;
     var number : int;
 }
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 Bunny83 · Jun 03, 2012 at 11:35 AM 2
Share

I just read your comment. It would be very helpful when you add this little piece of information to your question by editing it.

So basically you don't want a Vector3 (that contains 3 float values), so you have to use your own type. Also, like @sooncat mentioned, you probably want to use DateTime ins$$anonymous$$d of String but that's up to you.

avatar image
1
Wiki

Answer by sooncat · Jun 03, 2012 at 02:53 AM

  1. Vector3 is a struct not a class

  2. you may use struct instead of Vector3, and store them in List.(It's more readable) for example:

List allScore;

struct MyScore { Time time; Time date; int score; }

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 deadfire56 · Jun 03, 2012 at 03:12 AM 0
Share

Thanks, I'm also using ArrayPrefs2 to store the highscores. How would I go about implementing $$anonymous$$yScore into that?

ArrayPrefs2: http://www.unifycommunity.com/wiki/index.php?title=ArrayPrefs2

avatar image sooncat · Jun 03, 2012 at 04:54 AM 0
Share

Unfortunatly no exist function in ArrayPrefs2 could store struct/list directly. you may add your own function to store them or change structdata/listdata to string.

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

10 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

Related Questions

how to drop an object when the object is set in Camera.main.ScreenToWorldPoint 0 Answers

Getting vectors on an object every so many units 0 Answers

private Vector3 function? 2 Answers

Script not function if mouse is on object 1 Answer

Instantiate an object every x meters between two others. 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