[Question] C++ Reference StaticMeshComponent

I am having some trouble when trying to reference a StaticMeshComponent from a Blueprint. In short what I am trying to do is change the parameter of one of the materials, that part works fine. But when I try to then apply the material to the mesh I crash, I did now add in a check for my pointer and I am not crashing anymore but I am not sure what I am doing wrong exactly.

// Copyright 1998-2013 Epic Games, Inc. All Rights Reserved.

#pragma once

#include "VLight.generated.h"

UMaterialInstanceDynamic* LightMID;

class AVLight : public AActor
	UPROPERTY(EditAnywhere, BlueprintReadWrite, Category=Materials)
	UMaterialInterface* Base_Material;

	UPROPERTY(EditAnywhere, BlueprintReadWrite, Category=Meshes)
	UStaticMeshComponent* VaMesh;


	virtual void PostInitializeComponents() OVERRIDE;


// Copyright 1998-2013 Epic Games, Inc. All Rights Reserved.

#include "MyGamemode.h"
#include "VLight.h"

AVLight::AVLight(const class FPostConstructInitializeProperties& PCIP)
	: Super(PCIP)

void AVLight::PostInitializeComponents()

	LightMID = UMaterialInstanceDynamic::Create(Base_Material, this);
	LightMID->SetVectorParameterValue(FName(TEXT("Colour")), FLinearColor(1000,0,0,1));
    //For Testing only
    VaMesh->SetMaterial(1, LightMID);//Crashes

You have a c++ error


//For Testing only
    Mesh->SetMaterial(1, LightMID);//Crashes

should be this

//For Testing only
    VaMesh->SetMaterial(1, LightMID); //Happy

Make sure to modify the comment for good luck

That was corrected and the material is still not being applied.

Are you sure you want to set the second material (index 1)? Shouldn’t it be the first material (SetMaterial(0, LightMID))?

Yeah, I wish to apply the material in the second slot, I have also tried applying it to slot 0 but I am getting the same results.

are you sure that you are setting Base_Material in your BP for this class?

Does it show up in the blueprint defaults as a colored box / the image preview shows a material?


Yep that is working, I appears to be a problem either in my blueprint or the script. I can’t even run a GetMaterial for the mesh.

Try this:

UMaterialInstanceDynamic* LightMID = VaMesh->CreateAndSetMaterialInstanceDynamic(1);
LightMID->SetVectorParameterValue(FName(TEXT("Colour")), FLinearColor(1000.f,0.f,0.f,1.f));

VaMesh should have your Colourable material already applied.

That works, but that was not the problem. I finally got it working now, I had the mesh itself setup all wrong. It was not receiving the correct data from the BP of the class. I had to change some stuff in my .h file and it is all working now. Thank you everybody for your help!

could you post the corrected header file ?