- Home /
How do I make a script change a float value in a different script?
I have a game where randomly generated points pop up and you have to collect them. The points pop up when a variable called Points is equal to 0. I have an hp script for the points and i want the hp script to be able to bring the value of Points from 1 to zero. Sorry my question is confusing I do not really know how to word it. In recap I want a script to change the float value of an item in another script how can i do this. Thank you in advance.
Answer by Zoelovezle · Dec 24, 2015 at 11:37 AM
Your question was not confusing but yeah your description was :) Moving On ...
//This code will only valid if both of scripts are attached to same GameObject. i.e Your Player
//Suppose you have a script called PlayerPoints and in that you have a integer called totalPoints.
//Now in your new Script Say PlayerController
public class PlayerController : MonoBehaviour {
//Now first you need to declare a variable of your other Script
public PlayerPoints _playerPoint ;
//Now you need to get the PlayerPoints script , since it is a component
void Start()
{
_playerPoint = GetComponent<PlayerPoints>() ;
}
//now simply you can get any var from that script which are public..
public float clonedPoints ;
void Update()
{
clonedPoints = _playerPoint.totalPoints ;
}
Your answer
![](https://koobas.hobune.stream/wayback/20220612053724im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Change value according to Y position of an object? 2 Answers
Problem With float 2 Answers
Show Vector3 full float value in Debug 1 Answer
How to get float value from other script? 2 Answers
Positive to negative not working? 1 Answer