Search Unity

Bug Can't assign main camera to script

Discussion in 'Scripting' started by TheSnowyDev, Jan 10, 2021.

  1. TheSnowyDev

    TheSnowyDev

    Joined:
    Feb 17, 2020
    Posts:
    51
    I'm in Unity 2018.4.30f1, and I just wrote a script for a gun that shoots physics objects away with force. Anyway, I had to make a camera variable for this, but when I go to assign the Camera component in the inspector, I drag it over the field, it shows it can be set and then... absolutely nothing happens. The variable isn't assigned, there is no error thrown, and even using GetComponent, I still can't get it to work. Any ideas as to why this is happening?
     
  2. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,911
    This is a pretty basic feature of Unity and it would be very surprising to me if it is actually broken. Can you share some screenshots of what your inspector looks like and what exactly you're trying to drag into the slot?
     
  3. TheSnowyDev

    TheSnowyDev

    Joined:
    Feb 17, 2020
    Posts:
    51
  4. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,911
    Any chance you could share your ForceGun code?
     
  5. TheSnowyDev

    TheSnowyDev

    Joined:
    Feb 17, 2020
    Posts:
    51
    Sure thing:
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class ForceGun : MonoBehaviour
    6. {
    7.     float range = 50f;
    8.     float force = 100f;
    9.  
    10.     bool canShoot;
    11.  
    12.     public Camera playerCam;
    13.    
    14.     // Start is called before the first frame update
    15.     void Start()
    16.     {
    17.         //playerCam = GetComponent<Camera>();
    18.         if(playerCam == null)
    19.         {
    20.             Debug.LogError("No Camera");
    21.         }
    22.         canShoot = true;
    23.     }
    24.  
    25.     // Update is called once per frame
    26.     void Update()
    27.     {
    28.         if(Input.GetButtonDown("Fire1"))
    29.         {
    30.             Fire();
    31.         }
    32.     }
    33.  
    34.     void Fire()
    35.     {
    36.         RaycastHit hit;
    37.  
    38.         //Fires raycast and then checks whether the hit object has a Rigdbody
    39.         if(Physics.Raycast(playerCam.transform.position, playerCam.transform.forward, out hit, range))
    40.         {
    41.             Rigidbody hitRb = hit.transform.GetComponent<Rigidbody>();
    42.             if(hitRb != null)
    43.             {
    44.                 //Pushes away the hit gameObject
    45.                 hitRb.AddForce(playerCam.transform.forward * force);
    46.             }
    47.             Debug.Log(hit.transform.name);
    48.         }
    49.  
    50.     }
    51. }
     
  6. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,911
    Ok from everything you've shared here this is indeed strange. It should work with the setup you have...

    What happens if instead of dragging the component into the field, you drag the "Main Camera" object from the hierarchy window on the left side of the screen into that slot? That should also work.
     
  7. TheSnowyDev

    TheSnowyDev

    Joined:
    Feb 17, 2020
    Posts:
    51
    That wasn't working before, but apparently prefabs fix everything! UGH!
    Works like a charm now. Thanks for the help. Again. God, it's weird how many times our paths have crossed on the forum
     
  8. seejayjames

    seejayjames

    Joined:
    Jan 28, 2013
    Posts:
    691
    Odd, I tried the same script, no issues assigning the camera using either method.