Click here to Skip to main content
15,884,629 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
using System.Collections;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Diagnostics;
using System.Threading;
using UnityEngine;
using Debug = UnityEngine.Debug;

public class Laser : MonoBehaviour
{
    [SerializeField]
    private float __speed = 8f;
    // Start is called before the first frame update
    void Start()
    {
        
    }

    // Update is called once per frame
    void Update()
    {
        transform.Translate(Vector3.up*__speed * Time.deltaTime);
        Vector3 p = transform.position;
        if (transform.Translate.position>8f)
        {
            Destroy(this.gameObject);
        }
    }
}


What I have tried:

I tried to search this problem but i can't find any relavent solution for this.
i understood that i can't use transform.Translate.position in if statement.so i've tried to assign the Vector3 to other Vector3, then the error is displayed as '>' operater cant be used between Vector3's. Please help me with this.
Posted
Updated 1-Sep-20 8:03am
v2
Comments
[no name] 1-Sep-20 12:52pm    
transform.Translate.position is of type Vector3. You can't compare a Vector3 with a Float value e.g. 8f.

You can compare one of the components of the vector or the absulute value of the vector with 8f. But that depends really what you like to do here.

1 solution

It depends what you want to do. If you want to check the objects height then you need to check the y axis value

if (transform.position.y > 8f)
{
    Destroy(this.gameObject);
}


The other thing you might be wanting to check is the magnitude but that's unlikely, it's basically the length of the vector regardless of direction.

if (transform.position.magnitude > 8f)
{
    Destroy(this.gameObject);
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900