Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Voting for the Unity Awards are OPEN! We’re looking to celebrate creators across games, industry, film, and many more categories. Cast your vote now for all categories
    Dismiss Notice
  3. Dismiss Notice

Raycasting error: Is read-only

Discussion in 'Scripting' started by ellenblomw, Aug 23, 2018.

  1. ellenblomw

    ellenblomw

    Joined:
    Mar 4, 2018
    Posts:
    153
    Hi guys,

    I am trying to learn raycasting since OnMouseDown only works when attached to that gameobject, it's kinda limiting. So I am looking into raycasting but keep getting an error: "Assets/clickManager.cs(28,30): error CS0200: Property or indexer `UnityEngine.Component.gameObject' cannot be assigned to (it is read-only)"

    Code (CSharp):
    1.     [SerializeField]
    2.     GameObject backButton;
    3.  
    4.     // Use this for initialization
    5.     void Start () {
    6.      
    7.     }
    8.  
    9.     // Update is called once per frame
    10.     void Update () {
    11.         if (Input.GetMouseButtonDown(0))
    12.         {
    13.             Debug.Log("Klickade med musen");
    14.             //convert Input.mousePosition to world space which can then be used to compare against our GameObjects
    15.             Vector3 mousePos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
    16.             //make sure we skip z value by using 2D
    17.             Vector2 mousePos2D = new Vector2(mousePos.x, mousePos.y);
    18.             //zero says that only things hit exactly where we press are picked up
    19.             RaycastHit2D hit = Physics2D.Raycast(mousePos2D, Vector2.zero);
    20.  
    21.             //Now we can use the RaycastHit2D to determine if anything was hit by the click:
    22.             if (hit != null)
    23.             {            
    24.            
    25.                 if (hit.collider.gameObject == backButton){
    26.                     Debug.Log("Lets destroy stuff");
    27.                 }
    28.             }
    29.         }
    30.     }
    Do you know what I am doing wrong?
     
    Last edited: Aug 23, 2018
  2. ellenblomw

    ellenblomw

    Joined:
    Mar 4, 2018
    Posts:
    153
    Solved, should have used ==, not =. But I have a new problem. It does not register my Debug.Log("Lets destroy stuff"); when I press the assigned backButton. New code

    Code (CSharp):
    1.     void Update () {
    2.         if (Input.GetMouseButtonDown(0))
    3.         {
    4.             Debug.Log("Klickade med musen");
    5.          
    6.             //convert Input.mousePosition to world space which can then be used to compare against our GameObjects
    7.             Vector3 mousePos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
    8.          
    9.             //make sure we skip z value by using 2D
    10.             Vector2 mousePos2D = new Vector2(mousePos.x, mousePos.y);
    11.          
    12.             //zero says that only things hit exactly where we press are picked up
    13.             RaycastHit2D hit = Physics2D.Raycast(mousePos2D, Vector2.zero);
    14.  
    15.             //Now we can use the RaycastHit2D to determine if anything was hit by the click:
    16.             if (hit && hit.collider != null && hit.collider.gameObject == backButton)
    17.             {          
    18.                     Debug.Log("Lets destroy stuff");
    19.             }
    20.         }
    21.     }
    Anyone know why? :(
     
  3. Doug_B

    Doug_B

    Joined:
    Jun 4, 2017
    Posts:
    1,596
    One (or more) of the three conditions are not being met. You would need to debug to find out why. For example just debug.log out the value of all 3 parts individually. Which one is not as you expected?

    Having said that, if you are wanting a button, is there a reason you are not using a Unity UI button?
     
  4. ellenblomw

    ellenblomw

    Joined:
    Mar 4, 2018
    Posts:
    153
    Well at first it was because none of my UI elements showed up. But now I have UI dropdown etc working so I could make a new button under the UI canvas. But does that fix my problem?

    I removed the hit.collider == backButton from the script and then it seems to register. So I guess it has something to do with the gameObject/sprite.

    EDIT: I tried adding an UI button and assign that in the inspector. But it still doesn't register "Lets destroy stuff" in the console when I press it.
     
  5. Doug_B

    Doug_B

    Joined:
    Jun 4, 2017
    Posts:
    1,596
    If you use a UI Button, then you would just register an OnClick or OnPointerClick function. You wouldn't use a raycast with it at all. Just cleaner that way.
     
  6. ellenblomw

    ellenblomw

    Joined:
    Mar 4, 2018
    Posts:
    153
    Oh haven't used that method before. I'm pretty new to the whole UI thingy. I need the part script to be placed in another script because that's where I instantiate stuff that I then want to destroy if I press that certain button. The instantiate script is placed on another button.

    Eg:
    Button 1: Instantiates three sprites <-- Here is the script that instantiates and calls destruction of the clones
    Button 2: For the user to press to activate destroying of the instantiated clones

    How would I make the script understand if I press the button 2 or not if I don't use raycast? The OnPointerClick function would not be on the button that does the destroying call
     
    Last edited: Aug 23, 2018
  7. ThermalFusion

    ThermalFusion

    Joined:
    May 1, 2011
    Posts:
    906
    Register to the button's onClick callback.
    https://docs.unity3d.com/ScriptReference/UI.Button-onClick.html

    There is an example there, it does some weird things like getting the button component from the button component, just ignore that, but should otherwise show you how to connect to an external buttons click callback.
     
  8. ellenblomw

    ellenblomw

    Joined:
    Mar 4, 2018
    Posts:
    153
    OMG, that was so easy. Sorry for n00b questions haha. But I still wonder why my collider sprite wasn't registered? Any idea? I probably will have good use for raycast in the future so I would love to understand what I did wrong in that code.
     
  9. ThermalFusion

    ThermalFusion

    Joined:
    May 1, 2011
    Posts:
    906
    Since 2D lives in the main camera space and Screen UI lives in its own space, I think your raycasts just didn't hit your UI.
    The UI system has its own raycaster that should be used whenever you want to hit UI rather than Physics/Physics2D things.
     
    ellenblomw likes this.
  10. ellenblomw

    ellenblomw

    Joined:
    Mar 4, 2018
    Posts:
    153
    Ok thank you very much for your help, and also thank you to Doug!
     
    Doug_B likes this.