In the Hierarchy i have a FPSController and as child of it FirstPersonCharacter. I can get script that attached to the FPSController but not from the FirstPersonCharacter.


What i want to get is the Blur script from the FirstPersonCharacter.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityStandardAssets.Characters.FirstPerson;

public class FadeScript : MonoBehaviour
    public CharacterController firstPersonCharacter;
    public FirstPersonController fpc;
    public float fadeDuration = 5;
    public float speed;

    private Material material;
    private float targetAlpha = 0;
    private float lerpParam;
    private float startAlpha = 1;
    private bool rotated = false;

    void Start()
        material = GetComponent<Renderer>().material;

        fpc.enabled = false;

    void Update()
        lerpParam += Time.deltaTime;

        float alpha = Mathf.Lerp(startAlpha, targetAlpha, lerpParam / fadeDuration);

        if (alpha == 0)
            fpc.enabled = true;

            if (rotated == false)
                fpc.GetComponent<FirstPersonController>().enabled = false;
                fpc.transform.localRotation = Quaternion.Slerp(fpc.transform.rotation, Quaternion.Euler(0, 0, 0), speed * Time.deltaTime);

            if (fpc.transform.localRotation == Quaternion.Euler(0,0,0))
                fpc.GetComponent<FirstPersonController>().enabled = true;
                rotated = true;

    public void FadeTo(float alpha, float duration)
        startAlpha = material.color.a;
        targetAlpha = alpha;
        fadeDuration = duration;
        lerpParam = 0;

    private void SetMaterialAlpha(float alpha)
        Color color = material.color;
        color.a = alpha;
        material.color = color;

I can get scripts from the FPSController like this:

fpc.GetComponent<FirstPersonController>().enabled = true;

But i can't get the Blur script from the firstPersonCharacter. I tried before instead:

public CharacterController firstPersonCharacter;

With GameObject:

public GameObject firstPersonCharacter;

But Blur is not exist. When i make GetComponent The Blur is not exist.


1 Answer 1


FirstPersonCharacter itself does not have Blur component, but the GameObject that has it attached does:

[SerializeField] FirstPersonController FPC; /// <summary>Your First Person Controller.</summary>
/// Option 1.
private Blur blur; /// <summary>Blur's Script.</summary>
/// Option 2.
[Serializefield] GameObject objWithPFCAndBlur; /// <summary>So you could get both components from this GameObject.</summary>

void Awake()
    blur = FPC.gameObject.GetComponent<Blur>();
    if(blur == null)
    { /// As a good practice, you should do null checkings, on cases like this.
         Debug.LogError("There was no Blur attached to GameObject");
    else /// Continue...

Maybe you had problems because Components (such as Rigidbodys, MonoBehaviours, Colliders, etc) can have Components attached (so you can use GetComponent() method), but what usually you do on inspector is attaching Components to a GameObject, which was the case, FPC does not have a Blur attached, as you can see on your GameObject's inspector.

Hope that solves your issue.


You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .