Search Unity

UI Slider not adjusting rotation of GameObject?

Discussion in 'Scripting' started by larswik, Nov 24, 2016.

  1. larswik

    larswik

    Joined:
    Dec 20, 2012
    Posts:
    312
    Hi, I have been racking my brain a few hours now and google searching for an answer. Basically I have a UI Slider that should rotate a cannon from 0 to -90. It's set up and connected correctly. I use a Debug.Log(); to return the value on the cannon script and it works. But that is where it ends, it will not rotate the cannon.


    The cannon is a child object of a Castle game object but that should not matter?

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using UnityEngine.UI;
    4.  
    5. public class cannonController : MonoBehaviour {
    6.  
    7.     float angleValue;
    8.  
    9.     void Start () {
    10.         angleValue = 0;
    11.     }
    12.  
    13.     void Update () {
    14.         transform.Rotate(0,0,angleValue);
    15.     }
    16.  
    17.     public void AdjustCannonDegrees(float degreeNum){
    18.         angleValue = degreeNum;
    19.         Debug.Log(angleValue); // This works and the console displayed the value from the slider
    20.     }
    21. }
    Any ideas?
     
  2. larswik

    larswik

    Joined:
    Dec 20, 2012
    Posts:
    312
    Update: So I solved the problem and got the cannon to rotate with this code, but when I create a prefab, and instantiate the new castle with attached cannon the slider no longer works on the instantiated cannon?

    This code here works on the game objects on the screen before I make a prefab out of it.
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using UnityEngine.UI;
    4.  
    5. public class cannonController : MonoBehaviour {
    6.  
    7.     public float angleValue = 0f;
    8.  
    9.     void Start () {
    10.      
    11.     }
    12.  
    13.     void Update () {
    14.     }
    15.  
    16.     public void AdjustCannonDegrees(float degreeNum){
    17.         transform.eulerAngles = new Vector3(0, 0, degreeNum);
    18.         Debug.Log("Value=" + degreeNum);
    19.  
    20.     }
    21. }
    A little more description here. I drag a Castle image to the Hierarchy. Then I drag a cannon image to the Hierarchy and make that a child object of the castle. I rig up the UI Slider so it so it rotates up and down the cannon object. All of that works just fine. But now to join everything together I drag the castle with children from the Hierarchy to a prefab folder in the project. This turns the castle object into a blue prefab object.

    When I run the game and click on the screen it instantiates the castle prefab object, but the slider won't work on the instantiated object? But with the game still running, I click on the castle object in the prefab folder and I can see the z rotation in the inspector move. Some how when I instantiate the castle object it looses the connection to the slider?

    Any ideas?
     
  3. Zaflis

    Zaflis

    Joined:
    May 26, 2014
    Posts:
    438
    You didn't show any code or other info related to the connection. There's propably something wrong with it. What calls AdjustCannonDegrees(), and is it referring to right object? Did you debug what it refers to?
     
    LeftyRighty likes this.
  4. larswik

    larswik

    Joined:
    Dec 20, 2012
    Posts:
    312
    Gladly show that connection, I didn't think it was relevant since the code works before I make a prefab. In the Image included you can see the Slider, in the inspector, the On Value Change I selected the Cannon object with the attached cannon controller script. That script houses the AdjustCannonDegrees Code.

    I was originally following this youTube tut but for speed but I needed cannon rotation. That's when I discovered I needed eluerAngles.

    The only other code I use is code for my iPhone to drop a castle prefab. I am using Remote 5 to test it and this code works to drop a castle object where I touch the iPhone screen.

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class GameScript : MonoBehaviour {
    5.  
    6.     public GameObject castelPrefab;
    7.     public Camera cameraObj;
    8.     void Start () {
    9.  
    10.     }
    11.  
    12.     // Update is called once per frame
    13.     void Update () {
    14.         if((Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Began)){
    15.             if(GameObject.FindWithTag("castel_Right")){
    16.                 print("Castel already exists");
    17.             }
    18.             else{
    19.                 instantiateCastel(Input.GetTouch(0).position);
    20.             }
    21.         }
    22.     }
    23.  
    24.     void instantiateCastel(Vector2 pos){
    25.         //print("mouseposition= "+ Input.GetTouch(0).position);
    26.         Vector3 camPos = cameraObj.transform.position; // Get camera position
    27.         Vector3 tp = cameraObj.ScreenToWorldPoint(new Vector3(pos.x, 50f, 200f)); // adjust iphone touch to scrren location
    28.  
    29.         if(camPos.x > 0f){
    30.             castelPrefab.transform.localRotation = Quaternion.Euler(0, 0, 0);
    31.             Instantiate(castelPrefab, tp, castelPrefab.transform.rotation);
    32.         }
    33.         else{
    34.             castelPrefab.transform.localRotation = Quaternion.Euler(0, 180, 0);
    35.             Instantiate(castelPrefab, tp, castelPrefab.transform.rotation);
    36.         }
    37.     }
    38. }
     

    Attached Files:

  5. larswik

    larswik

    Joined:
    Dec 20, 2012
    Posts:
    312
    I spent to long trying to figure out this issue. I took a different approach that will work. Curious if this will be a bug in the future that gets resolved or something that I missed.
     
  6. Suddoha

    Suddoha

    Joined:
    Nov 9, 2013
    Posts:
    2,824
    Does the slider exist per instance? Is it part of the prefab?
    If not, then it looks like you've linked the prefab rather than the actual instances.

    I doubt there's a bug.
     
  7. Zaflis

    Zaflis

    Joined:
    May 26, 2014
    Posts:
    438
    Will it still work if you make 3 instances of the prefab, and then see 3 sliders too? Assuming the slider is part of the prefab, because that's the only way it would automatically be linked. If there only ever is 1 slider, you would need to do the linking manually by code. Like adding a listener
    https://docs.unity3d.com/ScriptReference/UI.Slider-onValueChanged.html