Unity position

using UnityEngine;
using System.Collections;

public class HDbar : MonoBehaviour {

	public float currentTime;
	
	// Use this for initialization
	void Start () {
		renderer.enabled = true;
		
	}
	
	// Update is called once per frame
	void Update () {
		
		currentTime += Time.deltaTime ;
		
		if(currentTime >= 5)
		{
			renderer.enabled = false;
			
		}
		
		//transform.position = new Vector3(0, 40, 0);
		transform.Translate (0,  0+ Time.deltaTime  , 0);
		//transform.Translate (0.0f, 40.0f, 0.0f);
	
		
		
	}
	
	void onGUI()
	{
		
				
	}
}

For this code, it has been attached to the main camera and was used to print out a rectangular boxes when user press a button. But for unknown reason, if I try to set the position of the box, but it always remain the same place despite i had use this code //transform.position = new Vector3(0, 40, 0);. Not only the position, I also got problem with the rotation where if I enter -90 into the unity scene itself(not script)by changing number, the rotation of the box would still remained the same. And for unknown reason, if I scale the box in unity scene itself by changing number , it work but not the rotation and position. Finally, if I do something like transform.Translate (0, 0+ Time.deltaTime , 0); the object is able to work and move upwards for 5 seconds before disappearing but that is not really I want. Despite setting the position of the object, the object would still remained at the same posiiton but when i try to add in time.delta.time, the box is able to move or rotate but what I really want is to move into my desired position and remained statinonary there for 5 seconds. Very funny. Help.

Try this :

        float translation = Time.deltaTime * 1;
        transform.Translate(0, translation, 0);

It is not working, the result is still the same where the boxes would still moves upwards.

Try using Vector3.MoveTowards for a constant speed or Vector3.Lerp if you want the object to slow down as it near its destination. Then you can do a Vector3.Distance to check if your object is close to its destination and set the time delay before you do your fading. Something like this:

void Update()
{
	if(Vector3.Distance(transform.position, location.transform.position) > 0.1f) // Assuming location is an empty gameobject in world space
	{
		transform.position = Vector3.MoveTowards(transform.position, location.transform.position, Time.deltaTime);
	}
	else
	{
		if(timeDelay < 5f) // Check if your delay time is less than 5 sec
		{
			timeDelay += Time.deltaTime;
		}
		else
		{
			if(Mathf.abs(this.renderer.material.color.a - fadeColor.a) > 0.01f) // Check if you haven't fade out yet
			{
				this.renderer.material.color = Color.Lerp(this.renderer.material.color, fadeColor, Time.deltaTime);
			}
		}
	}
}

I’m basing this from your last sentence in your first post.

I dun wan the box object to move around, my objective is to let the boxes stationary at a desired position for 5 seconds before it disappeared. I managed to make the object disappeared after 5 seconds but not sure how to make the object stationary for 5 seconds. The code i have done above are still mmoving up for 5 seocnds before disappearing which I did not want that.

Just do the time delay after the distance check. When the time delay hits 5 seconds, just run the fade.

Try to input your code, but not sure how to implement your code. How to declare the location variable first and the mathf

I already showed you the codes to do it.

        if(timeDelay < 5f) // Check if your delay time is less than 5 sec
        {
            timeDelay += Time.deltaTime;
        }
        else
        {
            if(Mathf.abs(this.renderer.material.color.a - fadeColor.a) > 0.01f) // Check if you haven't fade out yet
            {
                this.renderer.material.color = Color.Lerp(this.renderer.material.color, fadeColor, Time.deltaTime);
            }
        }

You just need to do your distance check which I’ve already shown you how to do it.

 if(Vector3.Distance(transform.position, location.transform.position) > 0.1f) // Assuming location is an empty gameobject in world space
    {
        transform.position = Vector3.MoveTowards(transform.position, location.transform.position, Time.deltaTime);
    }

But in your case, you’ll probably just want to check if it’s less than or equal to a small value (like 0.1) and do your delay and fade. And only make it move when it is more than that small value (0.1) which I have already shown you in my code.

location is a public game object variable that you drag and drop into the inspector.
fadeColor is your color you want to fade to. You can just set it to the same color as the current renderer’s color and set the alpha to 0 (transparent).
timeDelay is a float variable. It should be set to 0 in Start. Or whenever you want to reset the delay once.

I got 1 error, the mathf does not have a definition for abs, how should I solved it.

Capital A. My bad, typed on notepad.

Omg when I try to implement your code, my whole game scene just stuck and I cant move around and I cant maximise on play when testing the game scene. What is wrong with the code that I cant move around.

Did your Unity freeze? Is it still compiling when you try to run it? There shouldn’t be any infinite loop in my code implementation. What’s your current code? If your Unity is not frozen and your object is not moving, are there any errors in the console?

using UnityEngine;
using System.Collections;

public class HDbar : MonoBehaviour {

	public float currentTime;
	public float timeDelay;
	public Color fadeColor; 
	public GameObject location;
	
	// Use this for initialization
	void Start () {
		renderer.enabled = true;
		//transform.position = new Vector3(0, 40, 0);
	}
	
	// Update is called once per frame
	void Update () 
	{
		
		timeDelay = 0.0f;
		currentTime += Time.deltaTime ;
		
		if(currentTime >= 5)
		{
			renderer.enabled = false;
			
		}
		
		//transform.position = new Vector3(0, -40, 0);
		//transform.Translate (0,  Time.deltaTime  , 0);
		//transform.Translate (0.0f, 40.0f, 0.0f);
		
	//	float translation = Time.deltaTime * 1;
		//
      //  transform.Translate(0, translation, 0);
	
	
		 if(Vector3.Distance(transform.position, location.transform.position) > 0.1f) // Assuming location is an empty gameobject in world space
	
	    {
	
	        transform.position = Vector3.MoveTowards(transform.position, location.transform.position, Time.deltaTime);
	
	    }
	
	    else
	
	    {
	
	        if(timeDelay < 5f) // Check if your delay time is less than 5 sec
	
	        {
	
	            timeDelay += Time.deltaTime;
	
	        }
	
	        else
	
	        {
	
	            if(Mathf.Abs(this.renderer.material.color.a - fadeColor.a) > 0.01f) // Check if you haven't fade out yet
	
	            {
	
	                this.renderer.material.color = Color.Lerp(this.renderer.material.color, fadeColor, Time.deltaTime);
	
	            }
	
	        }
	
	    }
		
	}
	
	
	void onGUI()
	{
		
				
	}
}

Tell me if I have done anything wrong in my the code I have done. And about the gameObject Location, do I put in the bar inside or other thing, not sure about the location thingy. The code is able to run but maybe there might be some freeze inside.

Put timeDelay = 0.0f in Start, not in Update. You are resetting timeDelay every frame.

Done. Still not working. Maybe this one cause the problem. UnassignedReferenceException: The variable location of ‘HDbar’ has not been assigned.
You probably need to assign the location variable of the HDbar script in the inspector.
HDbar.Update () (at Assets/Script/HDbar.cs:41)

But if I removed your code, then my game would run normally and can move around, so I not sure why become like this.

UnassignedReferenceException: The variable location of ‘HDbar’ has not been assigned.
You probably need to assign the location variable of the HDbar script in the inspector.
HDbar.Update () (at Assets/Script/HDbar.cs:41)

Your location variable is an empty game object in the scene and it will be positioned to where your object that is moving will move to in your HDbar script.

hmm, result still the same, the object are still in the position as what I had coded. The object is not changing position at all.

It didn’t move at all? Where did you set your location game object in the scene? Otherwise, try multiplying Time.deltaTime in Vector3.MoveTowards. It might be moving but very slowly (as Time.deltaTime will give you a very small number).

No, no , i doesn’t mean that, my scene is able to move around already, but the problem is that the box object is still not changing the position(not moving) to the desired position I wanted. That mean that for unknown reason, I dun know why I can scale the object, but cant do the rotation and the translation for the object. perhaps the last post I never make it clear, my bad.