Search in
Sort by:

Question Status:

Search help

  • Simple searches use one or more words. Separate the words with spaces (cat dog) to search cat,dog or both. Separate the words with plus signs (cat +dog) to search for items that may contain cat but must contain dog.
  • You can further refine your search on the search results page, where you can search by keywords, author, topic. These can be combined with each other. Examples
    • cat dog --matches anything with cat,dog or both
    • cat +dog --searches for cat +dog where dog is a mandatory term
    • cat -dog -- searches for cat excluding any result containing dog
    • [cats] —will restrict your search to results with topic named "cats"
    • [cats] [dogs] —will restrict your search to results with both topics, "cats", and "dogs"

SetVectorParameterValue working only for the 1rst time?

Hey guys, I've been struggling with this for the past 2 days and havent been able to figure this out, im pretty sure is something really simple that I'm missing, so maybe anyone can help me :)

What im trying to do: Change a color through a function


 UPROPERTY(Category = Meshes, VisibleAnywhere)
 UStaticMeshComponent *segmentMesh;

 UPROPERTY(Category = Materials, EditAnywhere)
 UMaterialInterface *originalMaterial;//initialized in the details menu in the UE Editor
 UMaterialInstanceDynamic *segmentMaterialInstance;
 void ACitySegment::BeginPlay() {
     segmentMaterialInstance = UMaterialInstanceDynamic::Create(originalMaterial,this);
     if(segmentMaterialInstance->IsValidLowLevel()) {
         segmentMesh->SetMaterial(0, segmentMaterialInstance);
         segmentMaterialInstance->SetVectorParameterValue(FName(TEXT("SegmentColor")),FLinearColor(1,1,0, 1.0)); //<-- change the color to yellow
     } else {
         UE_LOG(LogTemp, Error, TEXT("Not valid material low level!"));

In theory after calling "BeginPlay" the color of the is yellow.

Later in the code I call this function:

 void ACitySegment::SetSegmentColor(const float &r, const float &g, const float &b) {
     segmentMaterialInstance = UMaterialInstanceDynamic::Create(originalMaterial,this);
     if(segmentMaterialInstance) {
         segmentMesh->SetMaterial(0, segmentMaterialInstance);
         segmentMaterialInstance->SetVectorParameterValue(FName(TEXT("SegmentColor")),FLinearColor(r, g, b, 1.0));
     } else {
         UE_LOG(LogTemp, Error, TEXT("Material instance not found!"));

With this value: SetSegmentColor(1,0,0); //<-- change the color to red

But the color stays Yellow! :S. Any idea what's happening here?

One more thing; I noticed that if I dont put any code in BeginPlay (regarding changing color) and then call the function SetSegmentColor() it changes its color how it should... :/ Thanks a bunch!

Product Version: UE 4.10
more ▼

asked Feb 10 '16 at 10:02 PM in C++ Programming

avatar image

1 2 2

(comments are locked)
10|2000 characters needed characters left
Viewable by all users

1 answer: sort voted first

When your BeginPlay function is called, you create a new DynamicMaterial having the originalMaterial as a parent. When SetSegmentColor is called you create a new DynamicMaterial having the new DynamicMaterial from BeginPlay as a parent. A DynamicMaterial can't be a parent for another DynamicMaterial.

Try getting rid of the creation of the DynamicMaterial in your SetSegmentColor function and keep the rest and that should fix your issue.

Let me know if that works.

more ▼

answered Feb 11 '16 at 04:45 AM

avatar image

13 3 4 6

avatar image naruse Feb 11 '16 at 08:42 AM

Thanks for the reply!, I tried that out, if I remove the

 segmentMaterialInstance = UMaterialInstanceDynamic::Create(originalMaterial,this);

inside SetSegment() I always get the error "Material instance not found!" from the UE_LOG :-/. Somehow the segmentMAterialInstance pointer ref is lost; could it be that is somehow garbage collected by the engine?.

(comments are locked)
10|2000 characters needed characters left
Viewable by all users
Your answer
toggle preview:

Up to 5 attachments (including images) can be used with a maximum of 5.2 MB each and 5.2 MB total.

Follow this question

Once you sign in you will be able to subscribe for any updates here

Answers to this question