- Home /
Question by
greenmee · Jan 19, 2015 at 07:40 AM ·
c#transformgetcomponent
I can't update my speed after reaching certain score
My score class
using UnityEngine;
using System.Collections;
public class Score : MonoBehaviour {
public GUIText scoreText;
public int scoreStart = 0;
public void scoreUpdate(){
scoreStart++;
scoreText.text = "Score: " + scoreStart;
//Debug.Log ("The updated score is "+scoreStart);
}
public void OnTriggerEnter (Collider col){
if (col.gameObject.tag == "Bullet") {
//Debug.Log ("Player hit");
scoreUpdate();
}
if (scoreStart == 10) {
Debug.Log ("update speed");
col.GetComponent<Bullet>().updateSpeed(100f);
}
}
}
My bullet class in which I want the speed to be updated
using UnityEngine;
using System.Collections;
public class Bullet : MonoBehaviour {
private float bulletSpeed;
void Start () {
bulletSpeed = 10f;
}
public void OnTriggerEnter(Collider col){
if (col.gameObject.tag == "Player") {
Debug.Log("Player get hit");
Destroy(gameObject);
}
}
// Update is called once per frame
void Update () {
transform.Translate(Vector3.right * -1 * Time.deltaTime * bulletSpeed);
Debug.Log ("current speed: " + bulletSpeed);
}
public void updateSpeed (float speed){
bulletSpeed += speed;
Debug.Log ("Upgraded Speed: " + bulletSpeed);
}
}
Comment
Answer by DaDonik · Jan 19, 2015 at 07:49 AM
You say that the bulletSpeed can only be updated if your scoreStart equals 10.
if (scoreStart == 10)
{
Debug.Log ("update speed");
col.GetComponent<Bullet>().updateSpeed(100f);
}
Since you increment scoreStart once every call to Update(), you won't be able to set the bulletSpeed after 10 frames.
Your answer
![](https://koobas.hobune.stream/wayback/20220613180825im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Distribute terrain in zones 3 Answers
Multiple Cars not working 1 Answer
How getting transform of another GameObject C#? 1 Answer
How to use "GetComponent" and "transform" code in dll file? 0 Answers