x

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"

Office Holiday

Epic Games' offices will be on holiday from June 22nd to July 7th. During this period support will be limited. Our offices will reopen on Monday, July 8th. 

How to change a static mesh's material in C++?

I have an Actor class with a SphereComponent and a StaticMeshComponent, and I wanted to change the Static Mesh's material when something overlaps with the SphereComponent. I don't have much experience with materials so I'm kinda stuck on this.

Product Version: UE 4.12
Tags:
more ▼

asked Jul 15 '16 at 01:05 AM in C++ Programming

avatar image

polomos
14 7 7 10

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

1 answer: sort voted first

Simply SetMaterial:

https://docs.unrealengine.com/latest/INT/API/Runtime/Engine/Components/UMeshComponent/SetMaterial/index.html

Easiest way to get material is to make UMaterial* varables for each with UPROPERTY(EditAnywhere,Category=Something) and set them in editor, you can also GetMaterial to get current material of mesh

Alternativly you can make one material with parameters that changes it's apperence

https://docs.unrealengine.com/latest/INT/Engine/Rendering/Materials/HowTo/Making_Parameters/

Here is example i find to change material parameters in C++: https://answers.unrealengine.com/questions/4323/questionedit-material-parameters-c.html

more ▼

answered Jul 15 '16 at 02:14 AM

avatar image

Shadowriver
36.5k 930 169 1111

avatar image polomos Jul 15 '16 at 04:52 PM

I tried the SetMaterial approach and it didn't work, I think it might be my Constructor here. PointNotCaptured being the UMaterial variable I made.

 // Sets default values
 AControlPoint::AControlPoint()
 {
      // Set this actor to call Tick() every frame.  You can turn this off to improve performance if you don't need it.
     PrimaryActorTick.bCanEverTick = true;
     SphereComponent = CreateDefaultSubobject<USphereComponent>(TEXT("Root Component"));
     RootComponent = SphereComponent;
     SphereComponent->InitSphereRadius(400.f);
 
     StaticMesh = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("Point"));
     StaticMesh->SetupAttachment(RootComponent);
     StaticMesh->SetMaterial(0, PointNotCaptured);
 }
 
avatar image polomos Jul 16 '16 at 06:55 PM

Wait nevermind, SetMaterial did work when I used it in a function. Though I am still confused why it didn't work in the constructor.

avatar image Blindopoly Apr 20 '19 at 01:17 AM

I was having the same problem, when I called SetMaterial in the constructor nothing would happen. I figured out setting the material directly on the static mesh and not the static mesh component worked...

 static ConstructorHelpers::FObjectFinder<UStaticMesh> plane_mesh(TEXT("StaticMesh'/Engine/BasicShapes/Plane.Plane'"));
 BorderMesh->SetStaticMesh(plane_mesh.Object);
 static ConstructorHelpers::FObjectFinder<UMaterial> plane_material(TEXT("Material'/Game/ClassicMansion/Materials/M_FlippedBorder.M_FlippedBorder'"));
 BorderMesh->GetStaticMesh()->SetMaterial(0, plane_material.Object);
(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