- Home /
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.
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?
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?
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.
Is it really worth spending so much time answering this question? :-)
@Fattie: I would say: if there's one person that got confused about what a Vector3 is, then there will be another one ;)
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;
}
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.
Answer by sooncat · Jun 03, 2012 at 02:53 AM
Vector3 is a struct not a class
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; }
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
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
Follow this Question
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