Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Dynamicly pass position of an object to other script

Discussion in 'Scripting' started by lubo5, Oct 21, 2020.

  1. lubo5

    lubo5

    Joined:
    May 17, 2020
    Posts:
    2
    Hello, I'm a newbie in unity scripting, but I am trying to do something like this:
    I have an object in Script 1, which is randomly generated within specific area. I have a Script 2, which generates movement from one waypoint to second, third.. etc. But I have also second script, which I want to use to add a waypoint basing on this object location. I want to pass this waypoint (which is changing together with object position) to second script, which will cause move of this moving object to this new waypoint (instead of old ones).
    So in short:

    Script 1:
    GameObject object;
    public bool isReady = false;

    then somewhere in Update method isReady = true;

    Script 2:
    public Vector3 function() {
    if (object.GetComponent<Script1>().isReady)
    {
    Vector3 newWaypoint = object.GetComponent<Script1>().object.transform.positon;
    return newWaypoint;
    }
    }
    in Update() method:
    myWaypoint = function();
    transform.position = Vector3.MoveTowards(transform.position, myWaypoint , m_speed * Time.deltaTime);

    I expect that now my moving object will go to newWaypoint, which is basing on object from Script1 position (which is changing all the time).

    But it's not working. Any ideas how to deal with that?
     
    Last edited: Oct 21, 2020
  2. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,888
    The answer to this question is "debugging".

    Use log statements, debuggers, etc. to figure out where your program is going off the rails and resolve it.

    The first place to start though would be a betteer description of "It's not working". That is not really an actionable statement. What's not working? The code isn't compiling? There's an error? The behavior is incorrect?
     
  3. lubo5

    lubo5

    Joined:
    May 17, 2020
    Posts:
    2
    Forgot to add: NullReferenceException: Object reference not set to an instance of an object when trying to call obj.GetComponent<Script1>()
     
  4. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,888
    Kurt-Dekker likes this.
  5. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,514
    Thanks for the reference Praetor! Specifically this is my whole blurb:

    Some notes on how to fix a NullReferenceException error in Unity3D
    - also known as: Unassigned Reference Exception
    - also known as: Missing Reference Exception

    http://plbm.com/?p=221

    The basic steps outlined above are:
    - Identify what is null
    - Identify why it is null
    - Fix that.

    Expect to see this error a LOT. It's easily the most common thing to do when working. Learn how to fix it rapidly. It's easy. See the above link for more tips.