- Home /
Vector4 and Color messing up with method overloading
Hello,
it's very hot here, and I can't seem to understand if my issue has a solution or not.
I have a method which accepts a parameter of type Color:
void SomeMethod(Color color) {...}
Now the problem is that I really really need to overload that method, so that it can alternatively accept a Vector4 parameter instead and do different things:
void SomeMethod(Vector4 vector) {...}
Now that's where things get bad, since Unity's Color type can be implicitly converted to Vector4 and viceversa, so the compiler doesn't know which one to choose. Anyone knows if there's some tricky solution to this issue, while still being able to overload?
Thanks
EDIT ------------------------
tswalk showed me that it SHOULD work, so I made other tests and realized the issue is still related to overloads and implicit casting, but happens only when there are optional parameters involved. Posted an updated question here
Answer by tswalk · Jul 10, 2014 at 03:39 PM
did you try casting the type to the overloaded method to see if it would get the right signature?
public void testMethod(Color color) { Debug.Log("color method"); }
public void testMethod(Vector4 _vector) { Debug.Log("vector method"); }
Vector4 test = new Vector4(0, 0, 0, 0);
Color cTest = Color.black;
testMethod((Vector4)test);
testMethod((Color)cTest);
testMethod(test);
testMethod(cTest);
In both sets of examples above, the correct method is called for me.... so, not sure why the signature isn't working for you.
This is weird, I have no issue with the replica of your example. Let me investigate a little more and I'll come back at you. In the meantime thanks a lot for the heads-up :)
no problem, i'm not an "expert" coder.. but perhaps if you posted a bit more code on the methods signatures and how you call them it may help.. perhaps it's just a typo :)
Yup I should've posted more code, sorry, but I thought the problem was language-based. Ins$$anonymous$$d apparently it's Unity/$$anonymous$$ono based. I'm gonna post a new question now with my new findings and redirect you there, in case you have something :)
Posted an updated question here: http://answers.unity3d.com/questions/745362/method-overloading-fails-with-implicit-typecasting.html
Your answer

Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Method overloading fails with implicit typecasting and optional parameters 0 Answers
Transitioning colors based on variable. 1 Answer
How to change a buttons color? 2 Answers