I am using this tutorial to develop an observer pattern for my game in unity. Here is the Observer
class:
using UnityEngine;
using System.Collections;
namespace ObserverPattern
{
//Wants to know when another object does something interesting
public abstract class Observer
{
public abstract void OnNotify();
}
public class Box : Observer
{
//The box gameobject which will do something
GameObject boxObj;
//What will happen when this box gets an event
BoxEvents boxEvent;
public Box(GameObject boxObj, BoxEvents boxEvent)
{
this.boxObj = boxObj;
this.boxEvent = boxEvent;
}
//What the box will do if the event fits it (will always fit but you will probably change that on your own)
public override void OnNotify()
{
Jump(boxEvent.GetJumpForce());
}
//The box will always jump in this case
void Jump(float jumpForce)
{
//If the box is close to the ground
if (boxObj.transform.position.y < 0.55f)
{
boxObj.GetComponent().AddForce(Vector3.up * jumpForce);
}
}
}
}
However, when I want to run this it gives me this error:
error CS0411: The type arguments for method 'GameObject.GetComponent()' cannot be inferred from the usage. Try specifying the type arguments explicitly.
The error is on this line:
boxObj.GetComponent().AddForce(Vector3.up * jumpForce);
Is there any possible way so that I can fix this error?
Thanks in advance
You need to add the templated argument in your case
boxObj.GetComponent<Rigidbody>().AddForce(Vector3.up * jumpForce);