Why can I not change the friction on runtime? And how can I fix it?
Hello. I am new at coding. I am making a 2D game where when the floor turns into blue, the floor gets slippery. You can see my script below. I attached this script to an empty game object; and to that game object, I attached the Floor script.
What am I doing wrong here? Can someone please explain and suggest a fix? You can also show a solution by changing the material itself, not the values. It would be highly appreciated.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class LevelChallenges : MonoBehaviour
{
public Floor myFloor;
private float frictionNumber;
public PolygonCollider2D coll;
public void Start()
{
frictionNumber = myFloor.GetComponent<PolygonCollider2D>().sharedMaterial.friction = 0.9f;
}
public void Update()
{
if (myFloor.isBlue)
{
frictionNumber = 0.2f;
}
else if (!myFloor.isBlue)
{
frictionNumber = 0.9f;
}
}
}
Your answer
![](https://koobas.hobune.stream/wayback/20220612222259im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
can't add script behaviour TMP_SelectionCaret . The script need to derive from MonoBehaviour 4 Answers
Problem with fresnel materials 0 Answers
Script to change GameObject materials based on tag? 2 Answers
Use a Raycast to get the Material from a shared Mesh (ProBuilder Object) 0 Answers
Jump Script in Unity 2D,I need help with jump script unity 2d 0 Answers