I just started learning to use Unreal Engine with C++, and while following the C++ Battery Collector tutorial serie on the Unreal Engine channel an error came up.
When attempting to set my RootComponet to my UStaticMeshComponet called BatteryMesh, Visual Studio returned an error saying
a value of type “UstaticMeshComponent *” cannot be assigned to an entity of type “USceneComponent *”
Below are my codes:
//in header file
private:
UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Catagory = "Battery", meta = (AllowPrivateAccess = "true"))
class UStaticMeshComponet* BatteryMesh;
//in constructor
BatteryMesh = CreateDefaultSubobject<UStaticMeshComponet>(TEXT("BatteryMesh"));
RootComponent = BatteryMesh; //error here
use
SetRootComponent(BatteryMesh);
(Not necessary but good practise)
Also Ignore the error log thats just Intellisense who has trouble understanding UE4 Code. If its Compiling successful you are good to go. If not check the error the Compiler gives you thats the one you can trust
Dno if you fixed it already but you wrote “UStaticMeshComponet” instead of “UStaticMeshComponent” notice the extra N at the end!
Componet
------------^
Component
EDIT:
After doing some more searching ( I was having the same issue), I found that this line does the work: USceneComponent(PickupMesh);
Aparently the RootComponent the tutorial talks about was changed or removed and a UStaticMeshComponent can no longer be assigned as a RootComponent. However, while using this it doesn’t show the Static Mesh menu in the "Class Defaults"when double clicking on the Blueprint. So there is still something wrong with the code, any help would be welcome!
One way to fix this issue and resolve the problem is to include the right class in the header of the CPP file. After numerous attempts to fix the problem with different ways I solved it by adding
In the CPP file. It can be assigned to USceneComponent but in order to do so it has to have the correct access path as above. Hope this helps as I can see this issue is a frequent one.