RayCast selection and palcement

So far I have this script:

using System.Collections.Generic;
using UnityEngine;
using System.Collections;

public class placer : MonoBehaviour
{
    Ray SRay;
    Ray myRay;
    RaycastHit myHit;
    RaycastHit SHit;
    public GameObject  WhatObject;


    private void Update()
    {
        var myRay = Camera.main.ScreenPointToRay(Input.mousePosition);

        if(Physics.Raycast(myRay, out myHit))
        {
            if (Input.GetMouseButtonDown(0))
            {
                Instantiate(WhatObject, myHit.point, Quaternion.identity);
            }

       
        }

       var SRay = Camera.main.ScreenPointToRay(Input.mousePosition);

        if(Physics.Raycast(SRay, out SHit))
        {
        [B]///HELP [/B]   
        }


    }
}

I want to assign an object hit by SHit (selection hit), to the GameObject ‘WhatObject’, using RightMouseButton, so I can place it using LeftMouseButton.
can somebody help me out please?

A couple of fields in RaycastHit contain references that you can use to get the GameObject. The easiest one is probably the .transform field, which also has a .gameObject shortcut.

WhatObject = SelHit.transform.gameObject;

ought to get you something useful.

thanks, I’ll try it

Thanks, I thought about transformation but I am working with C# only 2nd day now and I already took to many blind shots to take any more so I asked, anyway thanks a lot

1 Like

Hey, don’t knock blind shots, it’s a way to learn and go “Hm, what happened there?!” I do it all the time.