- Home /
Keep getting error Assets/ScreenShake.cs(20,44): error CS0411: The type arguments for method `UnityEngine.Component.GetComponent()' cannot be inferred from the usage. Try specifying the type arguments explicitly
Hello. I am trying to build a shake screen script using one I found online. I keep getting the error "The type arguments for method `UnityEngine.Component.GetComponent()' cannot be inferred from the usage. Try specifying the type arguments explicitly" If anyone could see where that would be coming from I would greatly appreciate it! I have marked the line that gives the error. Thanks in advance!
using UnityEngine; using System.Collections;
public class ScreenShake : MonoBehaviour {
//editor exposed variables
public float ShakeAmount = 0.25f;
public float DecreaseFactor = 1.0f;
//class internal variables
private new Camera camera;
private Vector3 cameraPos;
private float shake = 0.0f;
void Awake(){
//go and find the camera
this.camera = (Camera)this.GetComponent(); ............error line..............
if (this.camera == null) {
//print an error
Debug.Log ("CameraShake:Unable to find 'camera' component attached to GameObject");
}
}
// Update is called once per frame
void Update () {
//public void Shake(float amount){}
if (this.shake > 0.0f) {
//clamp the shake amount back to zero and reset the camera position to our chached value
this.shake = 0.0f;
this.camera.transform.localPosition = this.cameraPos;
}
}
public void Shake(float amount){
if(this.shake<=0.0f){
this.cameraPos = this.camera.transform.position;
}
this.shake = amount;
}
}
You could just expose the camera variable (make it public) and assign your camera in the inspector.
Guessing you can't call GetComponent in this fashion now, if you ever could. Try:
this.camera = GetComponent<Camera>() as Camera;
Answer by fffMalzbier · Mar 11, 2015 at 04:43 PM
The problem is that you did not specify what component you like to get.
a replacement for your line 20
camera = GetComponent<Camera>();
By the way, the "this" that you used everywhere is not necessary. You are already in the context of the class so "this" is not necessary.
Your answer
Follow this Question
Related Questions
Can't get more than one object. 1 Answer
Accessing all GameObjects with a certain tag 1 Answer
Expand script to stop camera from following player 2 Answers
Camera Problem! 2 Answers