using Unity3d 4.1.2 and C# Mono.
I have an object (player) that contains this script:
string[] colors = new string[] {"Red", "Blue", "Green", "Yellow", "Black", "Purple", "Pink", "Orange"};
public string joeColor;
// Use this for initialization
void Start ()
{
SetColorState();
}
// Update is called once per frame
void Update ()
{
}
public void SetColorState()
{
joeColor = colors[Random.Range(0, 7)];
}
void OnCollisionEnter(Collision obj)
{
PlatformMove platform = new PlatformMove();
platform = obj;
if(platform.platformColor.ToString() == joeColor.ToString())
{
Debug.Log("COLOR MATchED!!! Joe = " + joeColor.ToString() + " Platform COlor = " + platform.platformColor.ToString());
}
}
What I want to do is detect a collision on a platform object. Then get a method from that object (platform color) and if it same as my player object then = true.
As you can see the script above wont work, just been messing around seeing what would happen.
The platform object is randomly instantiated in the game, random color as well. So I need to detect that specific platform and then get its color state. What do I need to do?
Here is how to extract a component:
void OnCollisionEnter(Collision obj)
{
PlatformMove platform = obj.gameObject.GetComponent<PlatformMove>();
if(platform != null){
if(platform.platformColor.ToString() == joeColor.ToString()) {
//... do stuff
}
} else {
//... collision object did not have a PlatformMove component.
}
}
It appears you know how you want to handle the color comparison but if you anticipate a lot of collisions I would recommend switching from storing platformColor
as a string
to using an enum
instead.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.