0

The first script is attached to a empty GameObject.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

[System.Serializable]
public class SpinableObject
{
    public Transform t;
    public float rotationSpeed;
    public float minSpeed;
    public float maxSpeed;
    public float speedRate;
    public bool slowDown;
}
public class SpinObject : MonoBehaviour
{
    public SpinableObject[] objectsToRotate;
    private Rotate _rotate;
    private int index = 0;

    // Use this for initialization
    void Start()
    {
        _rotate = new Rotate>();
    }

    // Update is called once per frame
    void Update()
    {
        var _objecttorotate = objectsToRotate[index];
        _rotate.rotationSpeed = _objecttorotate.rotationSpeed;
        _rotate.minSpeed = _objecttorotate.minSpeed;
        _rotate.maxSpeed = _objecttorotate.maxSpeed;
        _rotate.speedRate = _objecttorotate.speedRate;
        _rotate.slowDown = _objecttorotate.slowDown;
    }
}

The second script is attached to the GameObject/s i want to feed with information. So this script is attached to each GameObject separate.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Rotate : MonoBehaviour
{
    public float rotationSpeed;
    public float minSpeed;
    public float maxSpeed;
    public float speedRate;
    public bool slowDown;

    // Use this for initialization
    void Start ()
    {

    }

    // Update is called once per frame
    void Update ()
    {
        RotateObject();
    }

    public void RotateObject()
    {
        if (rotationSpeed > maxSpeed)
            slowDown = true;
        else if (rotationSpeed < minSpeed)
            slowDown = false;

        rotationSpeed = (slowDown) ? rotationSpeed - 0.1f : rotationSpeed + 0.1f;
        transform.Rotate(Vector3.forward, Time.deltaTime * rotationSpeed);
    }
}

Not sure if this is a good way to what i want to do ?

Second problem is that the variable _rotate in the first script is all the time null:

I'm doing:

_rotate = new Rotate>();

But still here it's null:

_rotate.rotationSpeed = _objecttorotate.rotationSpeed;
daniel llee
  • 67
  • 1
  • 8
  • 3
    @daniel-lee : You have been on StackOverflow for 10 days and you have posted 11 questions. It seems you are asking the community to do all the job for you. – Hellium Jul 23 '17 at 15:39

1 Answers1

1

I don't think you understand how Unity works.

First, _rotate = new Rotate>(); is not valid C# and will throw an error.

Second, in your case Rotate is a MonoBehaviour which is not attached to a GameObject. I think that whatever you tried to accomplish is maybe a step to far. You can' synchronize the Update-call of a deattached Component (of which I don't even know if it gets its Update-method called) with another object at all. In short: Your code seems nonsense to me.

I'd suggest, you move your RotateObject method into the SpinableObject and call it from SpinObject, instead of shoving stuff into _rotate. This should work.

using System.Collections.Generic;
using UnityEngine;

[System.Serializable]
public class SpinableObject
{
    public Transform t;
    public float rotationSpeed;
    public float minSpeed;
    public float maxSpeed;
    public float speedRate;
    public bool slowDown;

    public void RotateObject()
    {
        if (rotationSpeed > maxSpeed)
            slowDown = true;
        else if (rotationSpeed < minSpeed)
            slowDown = false;

        rotationSpeed = (slowDown) ? rotationSpeed - 0.1f : rotationSpeed + 0.1f;
        t.Rotate(Vector3.forward, Time.deltaTime * rotationSpeed);
    }
}
public class SpinObject : MonoBehaviour
{
    [SerializeField]
    private SpinableObject[] objectsToRotate;

    // Update is called once per frame
    void Update()
    {
        foreach(var spinner in objectsToRotate)
            spinner.RotateObject();
    }
}
Max Play
  • 3,717
  • 1
  • 22
  • 39
  • This is working. And if i want to add some global variables ? I mean that the variables now will effect each transform alone but the global variables will effect all the transforms. If in the array for example there are 5 objects so if i will use the global variables it will effect on all the 5 gameobjects at the same time and if not using the global variable each gameobject in the array will use his own variables. Maybe using some bool like: globalEffect ? – daniel llee Jul 23 '17 at 15:25
  • Add the "global" variables to your `SpinObject`, and pass the values to `RotateObject()`. – Max Play Jul 23 '17 at 15:29