Search Unity

Question Raycast Color Change

Discussion in 'Getting Started' started by Split_22, Feb 13, 2024.

  1. Split_22


    Jan 30, 2024
    Hey so I'm trying to have it where when a button is pressed the color of the raycast object is changed.
    if (Input.GetMouseButtonDown(0) || Input.GetMouseButtonDown(1))
    //creates a vector to store the mouse position
    Vector3 clickPosition =;
    //A ray is casted from the postition of the camera through the mouse
    Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
    RaycastHit hit;
    // Spawns an object when it collides with something and overlaps
    if (Physics.Raycast(ray, out hit))
    clickPosition = hit.point;
    GameObject primitive = GameObject.CreatePrimitive(PrimitiveType.Sphere);
    primitive.transform.localScale = new Vector3(10, 10, 10);
    primitive.transform.position = clickPosition;
    This is the current code I have for the object being casted. I just want to give an option for the user so that they can change the color with a button press.
  2. arkano22


    Sep 20, 2012
    No free lunch, sorry. Not because we don't want to just give you the finished script, but because unless you try to do it yourself first you won't grow out of the "can't do anything without having someone else hand it to me" phase.

    Look into the Input class for detecting button presses, and into the Material class for changing object color. Come back to the forum with any questions that arise or if you need help understanding those.