Tell me the Issue With The Code Below And Provide An Alternative Implementation That Would Correct The Problem.
Using Unityengine;
Using System.collections;
Public Class Test : Monobehaviour {
Void Start () {
Transform.position.x = 10;
}
}?
Submitted by: MuhammadThe issue is that you can't modify the position from a transform directly. This is because the position is actually a property (not a field). Therefore, when a getter is called, it invokes a method which returns a Vector3 copy which it places into the stack.
So basically what you are doing in the code above is assigning a member of the struct a value that is in the stack and that is later removed.
Instead, the proper solution is to replace the whole property; e.g.:
using UnityEngine;
using System.Collections;
public class TEST : MonoBehaviour {
void Start () {
Vector3 newPos = new Vector3(10, transform.position.y, transform.position.z);
transform.position = newPos;
}
}
Submitted by: Muhammad
So basically what you are doing in the code above is assigning a member of the struct a value that is in the stack and that is later removed.
Instead, the proper solution is to replace the whole property; e.g.:
using UnityEngine;
using System.Collections;
public class TEST : MonoBehaviour {
void Start () {
Vector3 newPos = new Vector3(10, transform.position.y, transform.position.z);
transform.position = newPos;
}
}
Submitted by: Muhammad
Read Online Unity 3D Developer Job Interview Questions And Answers
Top Unity 3D Developer Questions
☺ | Explain me what Is The Function Of Inspector In Unity 3d? |
☺ | Tell us what Is An Unity3d File And How Can You Open A Unity3d File? |
☺ | Tell us in Unity 3d How Can You Hide Gameobject? |
☺ | Explain me total Sessions Today? |
☺ | Do you know what Is Prefabs In Unity 3d? |
Top Coding/Programming Categories
☺ | Python Interview Questions. |
☺ | OOP Interview Questions. |
☺ | Software engineering Interview Questions. |
☺ | PHP Interview Questions. |
☺ | VBA (Visual Basic for Applications) Interview Questions. |