This is a Unity (C #) question.
I want to make a script that moves the slider and the object moves, but it doesn't work.
NullReferrence ~ error has occurred, so I think the cause is that the slider value could not be obtained, but I do not know how to get the slider value on the canvas from another object, so the slider from another object I would like to know how to get the value of. By the way, we are planning to use a rigid body force.
"Problem script" ↓
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class ForceControrl: MonoBehaviour
{
// RigidBody definition
public Rigidbody rigidbody;
// Slider definition
Slider slider;
void Start ()
{
rigidbody = this.GetComponent
}
void Update ()
{
// Proceed (Force)
Vector3 force = new Vector3 (slider.value, 0, 0);
this.rigidbody.AddForce (force, ForceMode.Force);
}
}
-
Answer # 1
Related articles
- c # i want to pass a value to another method in the same class
- i want to reference a bool value in a script of another object
- c # - [unity] i am changing the value of a public variable from another script, but the value seen from the script with that pub
- python - i want to pass a value from view to form object in django
- if you update the database value with update in c # and search with select, it will be ???
- how to use the value every time the state of another file is updated in dart/flutter
- c # - the object is detected even though the range is specified i want to know how to specify a clear range
- c # - i want to display an object when the specified number of taps is reached
- python - i want to check the value stored in each object with django (= i want to debug)
- c # - [unity] object reference not set to an instance of an object error resolution
- in mysql, i want to determine the value of another column according to the value of one column
- i want to apply reduce to the value of a javascript object
- c # - error moving object by dragging
- i want to change the value of a specific key in the typescript list (object)
- c # - i want to press a button to activate the timer so that an object will be created after a certain amount of time has passed
- c # - [wpf] how to put a changing value in the binding path
- c # - i set an excel value using npoi, but it is not reflected in the reference
- javascript - i want to get the value associated with the array key in the object
- unity3d - i want to resize an object with the unity 3d slider
- c # - unity: i want to move an object according to the moving platform
- c # - player production in unity (shooting game)
- c # - i want to implement mario's mushroom-like behavior
- c # - programmatically find the coordinates of points in unity overtime
- how to write a unity c # interface
- c # - conversion from enum type to enum type
- c # - missingreferenceexception occurs even though there is a reference
- c # - i want to update the value
- c # - i want the camera to follow the player character in a tps game, but if i make multiple players in multiplayer, the camera
- c # - [unity] even though "has exit time" of animator is specified, it will transition to another state earlier than t
- c # - == becomes false even though the strings are equal
As for the script mentioned first, it is not good to use a variable name as it is, such as
Rigidbody rigidbody
Slider slider
. If you can't use it, it will come out. I think that it is good to keep it within the range that can be understood by using a part likeRigidbody rig
.Next is the
Slider slider
, but it is a defined but private variable, and no value is passed to the slider, so naturally it is empty when using the value. So it seems that a Null error has occurred.So make it a public variable and set the specified slider from the Inspector.
Next about
AddForce
, even if you can refer to the value of Slider as it is, it does not have enough force to move the object. (When Ues Gravity is enabled, if it is disabled, a method of quiescing is required.)The reason is that the value obtained from Slider is in the range of
0 to 1
, so the value is simply small.So, add power by multiplying the value obtained from Slider by any number.
A script that takes the above into account is as follows.
The
pulsPower
value can be adjusted with the slider from the Inspector, so try changing the value.