Error CS1061: 'Target' does not contain a definition for 'transform'

Daniel 1 Reputation point
2020-12-27T22:45:30.09+00:00

So in the Collector.cs class CS0161 is triggered at the first word "transform", but not the second.

Error CS1061 'Target' does not contain a definition for 'transform' and no accessible extension method 'transform' accepting a first argument of type 'Target' could be found (are you missing a using directive or an assembly reference?

The error is in the Collector.cs class but also involves Target.cs.

Collector.cs below:

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


/// <summary>
/// A collecting game object
/// </summary>
public class Collector : MonoBehaviour
{
 #region Fields

    // targeting support
    SortedList<Target> targets = new SortedList<Target>();
    Target targetPickup = null;

    // movement support
 const float BaseImpulseForceMagnitude = 2.0f;
    const float ImpulseForceIncrement = 0.3f;

 // saved for efficiency
    Rigidbody2D rb2d;


 #endregion

 #region Methods

 /// <summary>
 /// Use this for initialization
 /// </summary>
 void Start()
    {
 // center collector in screen
 Vector3 position = transform.position;
 position.x = 0;
 position.y = 0;
 position.z = 0;
 transform.position = position;

 // save reference for efficiency
 rb2d = GetComponent<Rigidbody2D>();

 // add as listener for pickup spawned event
 EventManager.AddListener(DelegateMethod);

 }

    private void DelegateMethod(GameObject arg0)
    {
 print("Pickup Spawned");
    }







    /// <summary>
    /// Called when another object is within a trigger collider
    /// attached to this object
    /// </summary>
    /// <param name="other"></param>
    void OnTriggerStay2D(Collider2D other)
    {
        // only respond if the collision is with the target pickup
 if (other.gameObject == targetPickup.GameObject)
        {
            // remove collected pickup from list of targets and game


 // go to next target if there is one

 }
 }


 /// <summary>
 /// Sets the target pickup to the provided pickup
 /// </summary>
 /// <param name="pickup">Pickup.</param>

 void SetTarget(GameObject pickup)
    {
 targetPickup = pickup;
 GoToTargetPickup();
 }

 /// <summary>
 /// Starts the teddy bear moving toward the target pickup
 /// </summary>
 void GoToTargetPickup()
    {
        // calculate direction to target pickup and start moving toward it
 Vector2 direction = new Vector2(
 targetPickup.transform (< Error).position.x - transform.position.x,
 targetPickup.transform(< Error).position.y - transform.position.y);
 direction.Normalize();
 rb2d.velocity = Vector2.zero;
 rb2d.AddForce(direction * BaseImpulseForceMagnitude, 
 ForceMode2D.Impulse);
 }


 #endregion
}

Target.cs class

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

/// <summary>
/// A target for the collector
/// </summary>
public class Target : IComparable
{
    #region Fields

    GameObject gameObject;
    float distance;

    #endregion

    #region Constructors

    /// <summary>
    /// Constructs a target with the given game object and the
    /// current position of the collector
    /// </summary>
    /// <param name="gameObject">target game object</param>
    /// <param name="position">collector position</param>
    public Target(GameObject gameObject, Vector3 position)
    {
        this.gameObject = gameObject;
        UpdateDistance(position);
    }

    #endregion

    #region Properties

    /// <summary>
    /// Gets the target game object
    /// </summary>
    /// <value>target game object</value>
    public GameObject GameObject
    {
        get { return gameObject; }
    }

    /// <summary>
    /// Gets the distance for the target
    /// </summary>
    /// <value>distance</value>
    public float Distance
    {
        get { return distance; }
    }



    #endregion

    #region Public methods

    /// <summary>
    /// Updates the distance from the target game object to
    /// the given position
    /// </summary>
    /// <param name="position">position for distance calculation</param>
    public void UpdateDistance(Vector3 position)
    {
        distance = Vector3.Distance(gameObject.transform.position,
            position);
    }

    /// <summary>
    /// Compares the current instance with another object of the same type 
    /// and returns an integer that indicates whether the current instance 
    /// precedes, follows, or occurs in the same position in the sort order
    /// as the other object.
    /// </summary>
    /// <returns>relative order of this instance and object</returns>
    /// <param name="obj">object to compare to</param>
    public int CompareTo(object obj)
    {
        // replace the code below with your implementation
        var target = (Target)obj;
        if (Distance == target.Distance)
        {
            return 0;
        }
        else if (Distance <= target.Distance)
        {
            return -1;
        }
        else 
        {
            return 1;
        }
    }

    /// <summary>
    /// Converts the target to a string
    /// </summary>
    /// <returns>the string for the target</returns>
    public override string ToString()
    {
        return distance.ToString();
    }
/* Temporarily added to avoid
Error CS0029 Cannot implicitly convert type 'UnityEngine.GameObject' to 'Target
    public static implicit operator Target(GameObject v)
    {
        throw new NotImplementedException();
    }
*/

    #endregion
}
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,357 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Daniel Zhang-MSFT 9,621 Reputation points
    2020-12-28T09:42:11.143+00:00

    Hi DanielYoo-1499,
    Here is a similar thread and you can refer to the Umair M's answer.
    Best Regards,
    Daniel Zhang


    If the response is helpful, please click "Accept Answer" and upvote it.

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments