Question by
ChileWilly · Oct 30, 2017 at 03:07 PM ·
raycastprefabgetcomponentinstanceenum
How to change the enum value of an instance of a prefab being clicked
Hi.
I'm having trouble working out how to change the enum value of a specific instance in my scene. Here is my code:
if (Input.GetMouseButtonDown(0))
{
RaycastHit2D hit = Physics2D.Raycast(Camera.main.ScreenToWorldPoint(Input.mousePosition), Vector2.zero);
if (hit.collider != null)
{
if (hit.collider.gameObject.tag == "BallDirector")
{
//NEED HELP HERE
myDirection = ChangeDirection(myDirection); // This doesnt work because it changes all instances
hit.collider.gameObject.GetComponent<Direction>().myDirection = ChangeDirection(myDirection) // It wont let me do this
}
}
}
Specifically when trying to call the enum value to change it.
Comment
Here is the whole script just in case:
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class BallDirector : $$anonymous$$onoBehaviour {
public enum Direction { North, NorthEast, East, SouthEast, South, SouthWest, West, NorthWest };
public Direction myDirection;
// Use this for initialization
void Start () {
myDirection = Direction.East;
}
// Update is called once per frame
void Update () {
GetInput();
Debug.Log(myDirection);
}
void GetInput()
{
if (Input.Get$$anonymous$$ouseButtonDown(0))
{
RaycastHit2D hit = Physics2D.Raycast(Camera.main.ScreenToWorldPoint(Input.mousePosition), Vector2.zero);
if (hit.collider != null)
{
if (hit.collider.gameObject.tag == "BallDirector")
{
//NEED HELP HERE
myDirection = ChangeDirection(myDirection); // This doesnt work because it changes all instances
hit.collider.gameObject.GetComponent<Direction>().myDirection = ChangeDirection(myDirection) // It wont let me do this
}
}
}
}
public Direction ChangeDirection(Direction dir)
{
switch(dir)
{
case Direction.North:
return Direction.NorthEast;
case Direction.NorthEast:
return Direction.East;
case Direction.East:
return Direction.SouthEast;
case Direction.SouthEast:
return Direction.South;
case Direction.South:
return Direction.SouthWest;
case Direction.SouthWest:
return Direction.West;
case Direction.West:
return Direction.NorthWest;
case Direction.NorthWest:
return Direction.North;
default:
return dir;
}
}
}